Skip to content

Commit

Permalink
added formula to geojson
Browse files Browse the repository at this point in the history
added formula to geojson
  • Loading branch information
dieuska committed Jun 26, 2024
1 parent f9028d5 commit 56374c9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
41 changes: 34 additions & 7 deletions brdr/aligner.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from brdr.geometry_utils import safe_union
from brdr.utils import diffs_from_dict_series, get_breakpoints_zerostreak, \
filter_resulting_series_by_key, get_collection, geojson_tuple_from_series, write_geojson, \
merge_geometries_by_theme_id, geojson_from_dict
merge_geometries_by_theme_id, geojson_from_dict, geojson_tuple_from_dict_theme

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S"
Expand Down Expand Up @@ -116,6 +116,7 @@ def __init__(
self.dict_relevant_intersection = None # dictionary to save relevant_intersections
self.dict_relevant_difference = None # dictionary to save relevant_differences

self.dict_predicted = None #dictionary with the 'predicted' results
# Coordinate reference system
# thematic geometries and reference geometries are assumed to be in the same CRS
# before loading into the Aligner. No CRS-transformation will be performed.
Expand Down Expand Up @@ -517,6 +518,7 @@ def predictor(
for zs in zero_streaks:
dict_predicted[key][zs[0]] = dict_series[zs[0]]
dict_predicted[key] = filter_resulting_series_by_key(dict_predicted[key],key)
self.dict_predicted = dict_predicted
return dict_predicted, diffs

def process_series(
Expand Down Expand Up @@ -667,28 +669,53 @@ def get_results_as_dict(self):
return (self.dict_result, self.dict_result_diff, self.dict_result_diff_plus, self.dict_result_diff_min,
self.dict_relevant_intersection, self.dict_relevant_difference)

def get_results_as_geojson(self):

def get_results_as_geojson(self,formula=False):
"""
get a geojson-tuple of the results
"""
prop_dictionary ={}
p = {}
for key in self.dict_result.keys():
formula = self.get_formula(self.dict_result[key])
p[key] = {'formula': formula}
prop_dictionary[self.relevant_distance] = p
return geojson_tuple_from_series({self.relevant_distance: self.get_results_as_dict()}, self.CRS,
self.name_thematic_id)
self.name_thematic_id, prop_dict=prop_dictionary)

def get_predictions_as_dict(self):
"""
get a dict with results for the predicted relevant distances
"""
return self.dict_predicted
def get_predictions_as_geojson(self,formula=False):
"""
get a geojson-tuple of the resulting geometries, based on the 'predicted' relevant distances.
Optional: The discriptive formula is added as an attribute to the result"""
prop_dictionary = dict(self.dict_predicted)
for key in prop_dictionary.keys():
for rel_dist in self.dict_predicted[key].keys():
formula = self.get_formula(self.dict_predicted[key][rel_dist][0][key])
p = None
if formula:
p = {key:{'formula': formula}}
prop_dictionary[key] = dict.fromkeys(self.dict_predicted[key].keys(), p)
return geojson_tuple_from_dict_theme(self.dict_predicted, crs=self.CRS, name_id=self.name_thematic_id,
prop_dict=prop_dictionary)

def get_reference_as_geojson(self):
"""
get a geojson of the reference polygons
"""
return geojson_from_dict(self.dict_reference, self.CRS, self.name_reference_id,geom_attributes=False)
def export_results(self, path, multi_to_single=True):
def export_results(self, path, formula=True):
"""
Exports analysis results as GeoJSON files.
This function exports 6 GeoJSON files containing the analysis results to the specified `path`.
Args:
path (str): The path to the directory where the GeoJSON files will be saved.
multi_to_single (bool, optional): If True (default), converts MultiPolygon geometries to single Polygons
in the exported GeoJSON files. This can be useful for visualization purposes.
Details of exported files:
- result.geojson: Contains the original thematic data from `self.dict_result`.
Expand All @@ -698,7 +725,7 @@ def export_results(self, path, multi_to_single=True):
- result_relevant_intersection.geojson: Contains the areas with relevant intersection that has to be included in the result.
- result_relevant_difference.geojson: Contains the areas with relevant difference that has to be excluded from the result.
"""
fcs = self.get_results_as_geojson()
fcs = self.get_results_as_geojson(formula=formula)
resultnames = [
"result.geojson",
"result_diff.geojson",
Expand Down
10 changes: 5 additions & 5 deletions brdr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def geojson_tuple_from_tuple(my_tuple, crs, name_id, prop_dict=None, geom_attrib
"""
feature_collections = []
for count, tup in enumerate(my_tuple):
feature_collections.append(geojson_from_dict(tup, crs, name_id, prop_dict=prop_dict))
feature_collections.append(geojson_from_dict(tup, crs, name_id, prop_dict=prop_dict,geom_attributes=geom_attributes))
return tuple(feature_collections)
def geojson_tuple_from_series(dict_series, crs, name_id, prop_dict=None, geom_attributes=True):
"""
Expand All @@ -31,10 +31,10 @@ def geojson_tuple_from_series(dict_series, crs, name_id, prop_dict=None, geom_at
for rel_dist in dict_series.keys():
my_tuple = dict_series [rel_dist]
prop_rel_dist = {'relevant_distance': rel_dist}
if prop_dict is not None and rel_dist in prop_dict:
prop_rel_dist = prop_rel_dist | prop_dict[rel_dist]
prop_dictionary = dict.fromkeys(my_tuple[0].keys(), prop_rel_dist)
fcs = geojson_tuple_from_tuple(my_tuple, crs, name_id, prop_dict=prop_dictionary)
if prop_dict is not None and rel_dist in prop_dict:
prop_dictionary = prop_dictionary | prop_dict[rel_dist]
fcs = geojson_tuple_from_tuple(my_tuple, crs, name_id, prop_dict=prop_dictionary, geom_attributes=geom_attributes)
for count, ft in enumerate(features):
ft.extend(fcs[count].features)
crs_geojson = {"type": "name", "properties": {"name": crs}}
Expand All @@ -50,7 +50,7 @@ def geojson_tuple_from_dict_theme(dict_theme, crs, name_id, prop_dict=None, geom
for key in dict_theme.keys():
if prop_dict is not None and key in prop_dict:
prop_dictionary = prop_dict[key]
fcs = geojson_tuple_from_series(dict_theme[key], crs, name_id, prop_dict=prop_dictionary)
fcs = geojson_tuple_from_series(dict_theme[key], crs, name_id, prop_dict=prop_dictionary, geom_attributes=geom_attributes)
for count, ft in enumerate(features):
ft.extend(fcs[count].features)
crs_geojson = {"type": "name", "properties": {"name": crs}}
Expand Down
2 changes: 1 addition & 1 deletion examples/example_131635.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
dict_results_by_distance = {}
#put resulting tuple in a dictionary
dict_results_by_distance[rel_dist] = aligner.process_dict_thematic(rel_dist,4)
aligner.export_results("output/")
aligner.export_results("output/",formula=True)
show_map(dict_results_by_distance, aligner.dict_thematic, aligner.dict_reference)
for key in dict_results_by_distance[rel_dist][0]:
print(key)
Expand Down
9 changes: 1 addition & 8 deletions examples/example_eo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@
series = np.arange(0, 200, 20, dtype=int)/100
#predict which relevant distances are interesting to propose as resulting geometry
dict_predicted, diffs = aligner.predictor(relevant_distances=series, od_strategy=2,treshold_overlap_percentage=50)
prop_dictionary = dict(dict_predicted)
for key in prop_dictionary.keys():
d = {}
for rel_dist in dict_predicted[key].keys():
formula = aligner.get_formula(dict_predicted[key][rel_dist][0][key])
d[rel_dist] = {'formula': formula}
prop_dictionary[key] = dict.fromkeys(dict_predicted[key].keys(), {'formula': formula})
fcs = geojson_tuple_from_dict_theme(dict_predicted, crs=aligner.CRS, name_id=aligner.name_thematic_id, prop_dict=prop_dictionary)
fcs = aligner.get_predictions_as_geojson()
write_geojson('output/predicted.geojson',fcs[0])
write_geojson('output/predicted_diff.geojson', fcs[1])
for key in dict_predicted.keys():
Expand Down

0 comments on commit 56374c9

Please sign in to comment.