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

Fix the bugs of generating topojson with complicated geojson #154

Merged
merged 3 commits into from
Apr 7, 2022
Merged
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
36 changes: 27 additions & 9 deletions topojson/core/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Extract(object):
object created including the keys `type`, `linestrings`, `coordinates` `bookkeeping_geoms`, `bookkeeping_coords`, `objects`
"""

def __init__(self, data, options={}):
def __init__(self, data, options={}):
# initation topology options
if isinstance(options, TopoOptions):
self.options = options
Expand Down Expand Up @@ -458,13 +458,24 @@ def _extract_featurecollection(self, geom):

# each Feature becomes a new GeometryCollection
for idx, feature in enumerate(obj["features"]):
# A GeoJSON Feature is mapped to a GeometryCollection
feature["type"] = "GeometryCollection"
feature["geometries"] = [feature["geometry"]]
feature.pop("geometry", None)
data["feature_{}".format(str(idx).zfill(zfill_value))] = geometry.shape(
feature
) # feature
# A GeoJSON Feature is mapped to a GeometryCollection => directly mapped to specific geometry, so that to save the attributes
feature["type"] = feature["geometry"]['type']
# feature["geometry"] = feature["geometry"]
# feature.pop("geometry", None)

feature_dict = {**(feature.get('properties') if feature.get('properties') else {}), **{'geometry' : geometry.shape(
feature['geometry']
)}}

if feature["type"] == 'GeometryCollection':
feature_dict['geometries'] = feature['geometry']['geometries']

### here not save the features for visualization:
data["feature_{}".format(str(idx).zfill(zfill_value))] = feature_dict # feature

# data["feature_{}".format(str(idx).zfill(zfill_value))] = geometry.shape(
# feature
# ) # feature

# new data dictionary is created, throw the geometries back to main()
self._is_single = False
Expand Down Expand Up @@ -630,7 +641,14 @@ def _extract_dictionary(self, geom):
# extract geometry and collect type and properties
geom = self._obj["geometry"]
self._obj.pop("geometry", None)
self._obj = {"properties": self._obj, "type": geom.geom_type}

if geom.geom_type == 'GeometryCollection':
geometries = self._obj["geometries"]
self._obj.pop("geometries",None)
self._obj = {"properties" : self._obj, "type" : geom.geom_type,'geometries':geometries}
else:
self._obj = {"properties": self._obj, "type": geom.geom_type}

self._data[self._key] = self._obj

# no direct shapely geometries available. Try forcing
Expand Down