Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a step to create an evenly-spaced version of categorical values #58

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pdgstaging/ConfigManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ class ConfigManager():
- simplify_tolerance : float
The tolerance to use when simplifying the input polygons.
Defaults to 0.0001. Set to None to skip simplification.
- categorical_values: dict
A dictionary mapping from the names of peroperties in the input
dataset representing categorical values to the order that the
values can be sorted into in order to be represented with
evenly-spaced values. This will be stored in a new column in
the staged dataset, with the prefix 'staged_normalized_' added
to the property's original name.

- Tiling & rasterization options.
- tms_id : str
Expand Down
29 changes: 29 additions & 0 deletions pdgstaging/TileStager.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def stage(self, path):
gdf = self.set_crs(gdf)
self.grid = self.make_tms_grid(gdf)
gdf = self.add_properties(gdf, path)
gdf = self.normalize_categorical_values(gdf)
self.save_tiles(gdf)
else:
logger.warning(f'No features in {path}')
Expand Down Expand Up @@ -422,6 +423,34 @@ def assign_tile(self, gdf):
predicate='intersects',
as_tile=True)

def normalize_categorical_values(self, gdf):
"""
Add a column converting each 'categorical_value' defined in the
config to an evenly-spaced set of values.

Parameters
----------
gdf: GeoDataFrame
The GeoDataFrame to add normalize the values for.

Returns
-------
GeoDataFrame
The GeoDataFrame with the normalized values added as a new
column.
"""
categorical_values = self.config.get('categorical_values')
for categorical_value in categorical_values.items():
prop = categorical_value[0]
order = categorical_value[1]
value_map = {}
i = 1
for element in order:
value_map[element] = i
i += 1
gdf['staged_normalized_' + prop] = gdf[prop].map(value_map)
return gdf

def make_tms_grid(self, gdf):
"""
Create a GeoDataFrame of rectangles that creates a TMS grid that
Expand Down