Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def write_shapefile(data, postcode_sector_name, filename):
# Translate props to Fiona sink schema
prop_schema = []
for name, value in data[0]['properties'].items():
fiona_prop_type = next((
fiona_type for fiona_type, python_type in \
fiona.FIELD_TYPES_MAP.items() if \
python_type == type(value)), None
)
prop_schema.append((name, fiona_prop_type))
sink_driver = 'ESRI Shapefile'
sink_crs = {'init': 'epsg:27700'}
sink_schema = {
'geometry': data[0]['geometry']['type'],
'properties': OrderedDict(prop_schema)
}
# Create path
directory = os.path.join(DATA_INTERMEDIATE,
'system_simulator', postcode_sector_name)
if not os.path.exists(directory):
for node_id, node in list(self._network.nodes('object')):
if node != None:
geom_list.setdefault(str(type(node)),[]).append(node)
for origin, dest in list(self._network.edges()):
link_obj = self._network[origin][dest]['object']
geom_list.setdefault(str(type(link_obj)),[]).append(link_obj)
# Write nodes to output
for key in geom_list.keys():
# Translate props to Fiona sink schema
prop_schema = []
for name, value in geom_list[key][0].props.items():
fiona_prop_type = next((fiona_type for fiona_type, python_type in fiona.FIELD_TYPES_MAP.items() if python_type == type(value)), None)
prop_schema.append((name, fiona_prop_type))
sink_schema = {
'geometry': geom_list[key][0].geom.type,
'properties': OrderedDict(prop_schema)
}
# Write all elements to output file
with fiona.open(os.path.join(directory, key[8:-2] + '.shp'), 'w', driver=sink_driver, crs=sink_crs, schema=sink_schema) as sink:
for node in geom_list[key]:
sink.write({
'geometry': mapping(node.geom),
'properties': OrderedDict(node.props)
})
def write_single_exchange_shapefile(data, path):
prop_schema = []
for name, value in data[0]['properties'].items():
fiona_prop_type = next((fiona_type for fiona_type, python_type in fiona.FIELD_TYPES_MAP.items() if python_type == type(value)), None)
prop_schema.append((name, fiona_prop_type))
sink_driver = 'ESRI Shapefile'
sink_crs = {'init': 'epsg:27700'}
sink_schema = {
'geometry': data[0]['geometry']['type'],
'properties': OrderedDict(prop_schema)
}
# Write all elements to output file
with fiona.open(path, 'w', driver=sink_driver, crs=sink_crs, schema=sink_schema) as sink:
[sink.write(feature) for feature in data]
def get_fiona_type(value):
for fiona_type, python_type in fiona.FIELD_TYPES_MAP.items():
if python_type == type(value):
return fiona_type
return None
def write_shapefile(data, path, crs):
# Translate props to Fiona sink schema
prop_schema = []
for name, value in data[0]['properties'].items():
fiona_prop_type = next((fiona_type for fiona_type, python_type in fiona.FIELD_TYPES_MAP.items() if python_type == type(value)), None)
prop_schema.append((name, fiona_prop_type))
sink_driver = 'ESRI Shapefile'
sink_crs = {'init':crs}
sink_schema = {
'geometry': data[0]['geometry']['type'],
'properties': OrderedDict(prop_schema)
}
# Write all elements to output file
with fiona.open(path, 'w', driver=sink_driver, crs=sink_crs, schema=sink_schema) as sink:
for feature in data:
sink.write(feature)