diff --git a/brdr/aligner.py b/brdr/aligner.py index 3b369ee..d184bcf 100644 --- a/brdr/aligner.py +++ b/brdr/aligner.py @@ -456,23 +456,14 @@ def process_dict_thematic( dict_result_diff_min[key] = result_diff_min dict_relevant_intersection[key] = relevant_intersection dict_relevant_diff[key] = relevant_diff - self.dict_result = merge_geometries_by_theme_id(dict_result) - self.dict_result_diff = merge_geometries_by_theme_id(dict_result_diff) - self.dict_result_diff_plus = merge_geometries_by_theme_id(dict_result_diff_plus) - self.dict_result_diff_min = merge_geometries_by_theme_id(dict_result_diff_min) - self.dict_relevant_intersection = merge_geometries_by_theme_id( - dict_relevant_intersection - ) - self.dict_relevant_difference = merge_geometries_by_theme_id(dict_relevant_diff) + self.dict_result = dict_result + self.dict_result_diff = dict_result_diff + self.dict_result_diff_plus = dict_result_diff_plus + self.dict_result_diff_min = dict_result_diff_min + self.dict_relevant_intersection = dict_relevant_intersection + self.dict_relevant_difference = dict_relevant_diff self.feedback_info("thematic dictionary processed") - 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, - ) + return self.get_results_as_dict(merged=False) def predictor( self, @@ -687,31 +678,44 @@ def get_last_version_date(self, geometry, grb_type=GRBType.ADP): update_dates = sorted(update_dates, reverse=True) return update_dates[0] - def get_results_as_dict(self): + def get_results_as_dict(self, merged=True): """ get a dict-tuple of the results """ - 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, - ) + if merged: + return ( + merge_geometries_by_theme_id(self.dict_result), + merge_geometries_by_theme_id(self.dict_result_diff), + merge_geometries_by_theme_id(self.dict_result_diff_plus), + merge_geometries_by_theme_id(self.dict_result_diff_min), + merge_geometries_by_theme_id(self.dict_relevant_intersection), + merge_geometries_by_theme_id(self.dict_relevant_difference), + merge_geometries_by_theme_id(self.dict_result), + ) + else: + 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, formula=False): + def get_results_as_geojson(self, formula=False, merged=True): """ 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} + tuple_results = self.get_results_as_dict(merged=merged) + dict_results = tuple_results[0] + for key in dict_results.keys(): + formula = self.get_formula(dict_results[key]) + p[key] = {"formula": json.dumps(formula)} prop_dictionary[self.relevant_distance] = p return geojson_tuple_from_series( - {self.relevant_distance: self.get_results_as_dict()}, + {self.relevant_distance: tuple_results}, self.CRS, self.name_thematic_id, prop_dict=prop_dictionary, @@ -733,7 +737,7 @@ def get_predictions_as_geojson(self, formula=False): formula = self.get_formula(self.dict_predicted[key][rel_dist][0][key]) p = None if formula: - p = {key: {"formula": formula}} + p = {key: {"formula": json.dumps(formula)}} prop_dictionary[key] = dict.fromkeys(self.dict_predicted[key].keys(), p) return geojson_tuple_from_dict_theme( self.dict_predicted, diff --git a/brdr/utils.py b/brdr/utils.py index b12fea5..0a8641d 100644 --- a/brdr/utils.py +++ b/brdr/utils.py @@ -42,8 +42,13 @@ def geojson_tuple_from_series( my_tuple = dict_series[rel_dist] prop_rel_dist = {"relevant_distance": rel_dist} prop_dictionary = dict.fromkeys(my_tuple[0].keys(), prop_rel_dist) - if prop_dict is not None and rel_dist in prop_dict: - prop_dictionary = prop_dictionary | prop_dict[rel_dist] + if ( + prop_dict is not None + and rel_dist in prop_dict + and prop_dict[rel_dist] is not None + ): + for key in prop_dictionary.keys(): + prop_dictionary[key] = prop_dictionary[key] | prop_dict[rel_dist][key] fcs = geojson_tuple_from_tuple( my_tuple, crs, @@ -505,17 +510,20 @@ def diffs_from_dict_series(dict_series, dict_thematic): results_diff = dict_series[rel_dist][1] for key in keys: if key not in results.keys() and key not in results_diff.keys(): - raise KeyError("No results calculated for theme_id " + str(key)) - - # calculate the diffs you want to have - # diff = results_diff[key].area * 100 / results[key].area #percentage of change - diff = ( - results[key].area - dict_thematic[key].area - ) # difference (m²) between area of resulting geometry and original geometry - diff = round(diff, 1) # round, so the detected changes are within 10cm² - # diff = abs(results[key].area - dict_thematic[key].area) #absolute difference (m²) between area of resulting geometry and original geometry - # diff = abs(results[key].area - dict_thematic[key].area)*100/dict_thematic[key].area #absolute difference (%) between area of resulting geometry and original geometry - # TODO: determine a good diff-value for determination + logging.info( + "No diff calculated for theme_id " + str(key) + ": diff set to zero" + ) + diff = 0 + else: + # calculate the diffs you want to have + # diff = results_diff[key].area * 100 / results[key].area #percentage of change + diff = ( + results[key].area - dict_thematic[key].area + ) # difference (m²) between area of resulting geometry and original geometry + diff = round(diff, 1) # round, so the detected changes are within 10cm² + # diff = abs(results[key].area - dict_thematic[key].area) #absolute difference (m²) between area of resulting geometry and original geometry + # diff = abs(results[key].area - dict_thematic[key].area)*100/dict_thematic[key].area #absolute difference (%) between area of resulting geometry and original geometry + # TODO: determine a good diff-value for determination diffs[key][rel_dist] = diff return diffs diff --git a/examples/example_multipolygon.py b/examples/example_multipolygon.py new file mode 100644 index 0000000..f562f1b --- /dev/null +++ b/examples/example_multipolygon.py @@ -0,0 +1,39 @@ +# Initiate brdr +import tests +from brdr.aligner import Aligner +from brdr.enums import OpenbaarDomeinStrategy +from brdr.utils import multipolygons_to_singles, write_geojson +from examples import show_map + +aligner0 = Aligner() + +# Load thematic data + +aligner0.load_thematic_data_file( + "../tests/testdata/multipolygon.geojson", "theme_identifier" +) +aligner0.dict_thematic = multipolygons_to_singles(aligner0.dict_thematic) +aligner0.load_thematic_data_dict( + aligner0.dict_thematic, +) +# gebruik de actuele adp-percelen adp= administratieve percelen +aligner = Aligner() +aligner.load_thematic_data_dict( + aligner0.dict_thematic, +) +aligner.load_reference_data_grb_actual(grb_type="adp", partition=1000) + +# Example how to use the Aligner +# rel_dist = 2 +# dict_results_by_distance = {} +# dict_results_by_distance[aligner.relevant_distance] = aligner.process_dict_thematic( +# relevant_distance=rel_dist, +# od_strategy=OpenbaarDomeinStrategy.SNAP_FULL_AREA_ALL_SIDE, +# ) +# aligner.export_results("output/") +# show_map(dict_results_by_distance, aligner.dict_thematic, aligner.dict_reference) +dict_predicted, diffs = aligner.predictor() +fcs = aligner.get_predictions_as_geojson(formula=True) +aligner.export_results("output/") +write_geojson("output/predicted.geojson", fcs[0]) +write_geojson("output/predicted_diff.geojson", fcs[1]) diff --git a/examples/examples_predictor.py b/examples/examples_predictor.py index cad8e05..1e0e04e 100644 --- a/examples/examples_predictor.py +++ b/examples/examples_predictor.py @@ -22,7 +22,7 @@ series = np.arange(0, 300, 10, dtype=int) / 100 # predict which relevant distances are interesting to propose as resulting geometry - dict_predicted = aligner.predictor( + dict_predicted, diffs = aligner.predictor( relevant_distances=series, od_strategy=4, treshold_overlap_percentage=50 ) for key in dict_predicted.keys(): diff --git a/tests/testdata/multipolygon.geojson b/tests/testdata/multipolygon.geojson new file mode 100644 index 0000000..821209d --- /dev/null +++ b/tests/testdata/multipolygon.geojson @@ -0,0 +1,8 @@ +{ +"type": "FeatureCollection", +"name": "themelayer", +"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::31370" } }, +"features": [ +{ "type": "Feature", "properties": { "id": 600, "theme_identifier": "600" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 172907.478557254769839, 171306.329770992975682 ], [ 172907.423273425694788, 171307.187050310545601 ], [ 172907.258293797436636, 171308.030809821182629 ], [ 172906.986220197344664, 171308.847742933343397 ], [ 172906.611343388562091, 171309.62496612383984 ], [ 172906.139575402339688, 171310.350222118664533 ], [ 172905.578356301615713, 171311.012073197518475 ], [ 172904.936536846886156, 171311.600081573560601 ], [ 172904.224238914379384, 171312.104974003392272 ], [ 172903.452695867919829, 171312.518788031826261 ], [ 172902.634075402340386, 171312.834997564437799 ], [ 172901.781287651334424, 171313.048615787964081 ], [ 172900.907781587186037, 171313.156273815431632 ], [ 172900.027332922356436, 171313.156273815431632 ], [ 172899.153826858208049, 171313.048615787964081 ], [ 172898.301039107202087, 171312.834997564437799 ], [ 172897.482418641622644, 171312.518788031826261 ], [ 172896.710875595163088, 171312.104974003392272 ], [ 172895.998577662656317, 171311.600081573560601 ], [ 172895.356758207926759, 171311.012073197518475 ], [ 172894.795539107202785, 171310.350222118664533 ], [ 172894.323771120980382, 171309.62496612383984 ], [ 172893.948894312197808, 171308.847742933343397 ], [ 172893.676820712105837, 171308.030809821182629 ], [ 172893.511841083847685, 171307.187050310545601 ], [ 172893.456557254772633, 171306.329770992975682 ], [ 172893.511841083847685, 171305.472491675405763 ], [ 172893.676820712105837, 171304.628732164768735 ], [ 172893.948894312197808, 171303.811799052607967 ], [ 172894.323771120980382, 171303.034575862111524 ], [ 172894.795539107202785, 171302.309319867286831 ], [ 172895.356758207926759, 171301.647468788432889 ], [ 172895.998577662656317, 171301.059460412390763 ], [ 172896.710875595163088, 171300.554567982559092 ], [ 172897.482418641622644, 171300.140753954125103 ], [ 172898.301039107202087, 171299.824544421513565 ], [ 172899.153826858208049, 171299.610926197987283 ], [ 172900.027332922356436, 171299.503268170519732 ], [ 172900.907781587186037, 171299.503268170519732 ], [ 172901.781287651334424, 171299.610926197987283 ], [ 172902.634075402340386, 171299.824544421513565 ], [ 172903.452695867919829, 171300.140753954125103 ], [ 172904.224238914379384, 171300.554567982559092 ], [ 172904.936536846886156, 171301.059460412390763 ], [ 172905.578356301615713, 171301.647468788432889 ], [ 172906.139575402339688, 171302.309319867286831 ], [ 172906.611343388562091, 171303.034575862111524 ], [ 172906.986220197344664, 171303.811799052607967 ], [ 172907.258293797436636, 171304.628732164768735 ], [ 172907.423273425694788, 171305.472491675405763 ], [ 172907.478557254769839, 171306.329770992975682 ] ] ], [ [ [ 172859.258396949415328, 171379.685496183810756 ], [ 172893.114885499031516, 171367.032061069301562 ], [ 172879.777480918884976, 171334.201526718155947 ], [ 172847.630916033376707, 171347.025954199081752 ], [ 172859.258396949415328, 171379.685496183810756 ] ] ], [ [ [ 172908.846183208952425, 171363.954198473889846 ], [ 172929.536259544838686, 171356.259541985346004 ], [ 172926.116412216593744, 171336.766412214346929 ], [ 172902.177480918879155, 171346.341984733415302 ], [ 172908.846183208952425, 171363.954198473889846 ] ] ], [ [ [ 172911.411068705143407, 171403.96641221435857 ], [ 172908.846183208952425, 171402.598473283054773 ], [ 172908.333206109731691, 171398.323664122755872 ], [ 172910.214122140256222, 171395.75877862656489 ], [ 172915.172900766221574, 171395.929770992981503 ], [ 172918.592748094466515, 171399.007633588393219 ], [ 172917.908778628800064, 171401.914503817417426 ], [ 172915.685877865442308, 171404.308396947191795 ], [ 172912.608015270030592, 171404.992366412829142 ], [ 172911.411068705143407, 171403.96641221435857 ] ] ] ] } } +] +}