Skip to content

Commit

Permalink
added functions for geojson
Browse files Browse the repository at this point in the history
function to get the results in geojson
  • Loading branch information
dieuska committed Jun 24, 2024
1 parent be97359 commit f9028d5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
50 changes: 36 additions & 14 deletions brdr/aligner.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
from brdr.geometry_utils import safe_symmetric_difference
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
filter_resulting_series_by_key, get_collection, geojson_tuple_from_series, write_geojson, \
merge_geometries_by_theme_id, geojson_from_dict

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S"
Expand Down Expand Up @@ -440,20 +441,20 @@ 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 = 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.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.feedback_info("thematic dictionary processed")
return (
dict_result,
dict_result_diff,
dict_result_diff_plus,
dict_result_diff_min,
dict_relevant_intersection,
dict_relevant_diff,
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 predictor(
Expand Down Expand Up @@ -659,6 +660,25 @@ 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):
"""
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)

def get_results_as_geojson(self):
"""
get a geojson-tuple of the results
"""
return geojson_tuple_from_series({self.relevant_distance: self.get_results_as_dict()}, self.CRS,
self.name_thematic_id)

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):
"""
Exports analysis results as GeoJSON files.
Expand All @@ -675,8 +695,10 @@ def export_results(self, path, multi_to_single=True):
- result_diff.geojson: Contains the difference between the original and predicted data from `self.dict_result_diff`.
- result_diff_plus.geojson: Contains results for areas that are added (increased area).
- result_diff_min.geojson: Contains results for areas that are removed (decreased area).
- 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 = geojson_tuple_from_series({self.relevant_distance: (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)}, self.CRS, self.name_thematic_id)
fcs = self.get_results_as_geojson()
resultnames = [
"result.geojson",
"result_diff.geojson",
Expand Down
3 changes: 3 additions & 0 deletions brdr/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@

#default CRS:
DEFAULT_CRS= "EPSG:31370"

#MULTI_SINGLE_ID_SEPARATOR #separator to split multipolygon_ids to single polygons
MULTI_SINGLE_ID_SEPARATOR = '*$*'
12 changes: 8 additions & 4 deletions brdr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from shapely import node
from shapely import polygonize
from shapely.geometry import shape
from brdr.constants import MULTI_SINGLE_ID_SEPARATOR

import brdr.constants


def geojson_tuple_from_tuple(my_tuple, crs, name_id, prop_dict=None, geom_attributes=True):
"""
Expand Down Expand Up @@ -125,7 +129,7 @@ def multipolygons_to_singles(dict_geoms):
continue
i = 0
for p in polygons:
new_key = str(key) + "_" + str(i)
new_key = str(key) + MULTI_SINGLE_ID_SEPARATOR + str(i)
resulting_dict_geoms[new_key] = p
i = i + 1
else:
Expand Down Expand Up @@ -421,12 +425,12 @@ def diffs_from_dict_series(dict_series, dict_thematic):
keys = dict_thematic.keys()
diffs = {}
for key in keys:
diffs[key]={}
diffs[key] = {}
for rel_dist in dict_series: #all the relevant distances used to calculate the series
results = dict_series[rel_dist][0]
results_diff =dict_series[rel_dist][1]
for key in keys:
if key not in results.keys() or key not in results_diff.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
Expand Down Expand Up @@ -486,7 +490,7 @@ def merge_geometries_by_theme_id(dictionary):
"""
dict_out = {}
for id_theme in dictionary:
id_theme_global = id_theme.split("_")[0]
id_theme_global = id_theme.split(MULTI_SINGLE_ID_SEPARATOR)[0]
geom = dictionary[id_theme]
if geom.is_empty or geom is None:
continue
Expand Down
4 changes: 2 additions & 2 deletions tests/test_aligner.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def test_process_geometry(self):

def test_predictor(self):
##Load thematic data & reference data
thematic_dict = {"theme_id_1": from_wkt('POLYGON ((0 0, 0 9, 5 10, 10 0, 0 0))')}
thematic_dict = {"theme_id": from_wkt('POLYGON ((0 0, 0 9, 5 10, 10 0, 0 0))')}
# ADD A REFERENCE POLYGON TO REFERENCE DICTIONARY
reference_dict = {"ref_id_1": from_wkt('POLYGON ((0 1, 0 10,8 10,10 1,0 1))')}
reference_dict = {"ref_id": from_wkt('POLYGON ((0 1, 0 10,8 10,10 1,0 1))')}
# LOAD THEMATIC DICTIONARY
self.sample_aligner.load_thematic_data_dict(thematic_dict)
# LOAD REFERENCE DICTIONARY
Expand Down

0 comments on commit f9028d5

Please sign in to comment.