diff --git a/topojson/core/extract.py b/topojson/core/extract.py index 61bfa6d..94b83b1 100644 --- a/topojson/core/extract.py +++ b/topojson/core/extract.py @@ -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 @@ -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 @@ -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