def write_geojson(features, outFileName, srs=None): features = geo_features(features) with open(outFileName,'w') as f: # FeatureCollection header f.write('{\n"type": "FeatureCollection",\n') # spatial ref spec if srs: f.write('"crs": ') json.dump(spatialref.geojson_crs(srs),f,indent=2) f.write(',\n') # features header f.write('"features": [\n') # features for feature in features: geojson.dump(feature,f,indent=2) if feature != features[-1]: f.write(',') f.write('\n\n') # close features f.write(']\n') # close FeatureCollection f.write('}\n')
def geo_feature_collection(geo, srs=None): """return a feature collection for multiple types of input. Add coordinate ref system, if srs arg given.""" # handle geo_interface try: geo = geo.__geo_interface__ except AttributeError: pass # input is already a feature collection? type = None try: type = geo['type'] except: pass isfc = (type=='FeatureCollection') if isfc: fc = geo else: features = geo_features(geo) fc = geojson.FeatureCollection(features) # add coordinate reference system, if srs supplied if srs: fc.crs = spatialref.geojson_crs(srs) return fc