[GIS] How best to use shapely to write a dict to a shapefile

fionapythonshapefileshapely

I have created a shapely object (i.e. the_pt) for my Points & Multipoints to do a buffer analysis against another shapely object (i.e. city_pt) using:

  return_shapes = []
  for pt in points:
            # create a shapely obj of all the points
            the_pt = shape(pt['geometry'])
            # what type of geometry is it?
            geom = pt['geometry']
            # check if it is a multipoint or point
            if geom['type'] == 'MultiPoint':
                # i.e. <class 'shapely.geometry.multipoint.MultiPoint'>
                pts = []
                for m_pt in the_pt:
                    city_pt_buffer = city_pt.buffer(dist)
                    # city_pt is a shapely obj
                    if m_pt.within(city_pt_buffer):
                        pts.append(m_pt)
                return_shapes.append(MultiPoint(pts))
            # check if it is a point geom
            elif geom['type'] == 'Point':
                    city_pt_buffer = city_pt.buffer(dist)
                    if the_pt.within(city_pt_buffer):
                        return_shapes.append(the_pt)
        return return_shapes

If I evaluate pt – I have a dict type with the geometry and properties needed to map to shapefile…

 <type 'dict'>
    {'geometry': {'type': 'Point', 'coordinates': (-13649075.348882342, 6193380.937753927)}, 'type': 'Feature', 'id': '7', 'properties': OrderedDict([(u'OBJECTID', 68547), (u'ISO', u'USA'), (u'SITE_NAME', u'Anacortes')])}

however what I really want is 'return_shapes' to be written to shapefile (with all the properties…Is there anyway to build return_shapes (a list) into something I can write (map) into a shapefile format?

Best Answer

The Fiona user manual has an example of creating a shapefile and writing exactly this kind of feature dict: https://fiona.readthedocs.io/en/latest/manual.html#writing-vector-data.