我已经下载了一个openstreetmap shapefile并希望提取某些功能(使文件不那么重,只有我感兴趣的那些,例如来自道路的自行车道)。我该怎么做,我现在可以打开文件并使用以下代码查看功能:
from osgeo import ogr
file = ogr.Open('gis.osm_roads_free_1.shp')
shape = file.GetLayer(0)
# here I want to filter on an "fclass" value
有关如何执行此操作的任何建议吗?
答案 0 :(得分:0)
试试这个。
from osgeo import ogr
def create_filtered_shapefile(value, filter_field, in_shapefile, out_shapefile):
input_layer = ogr.Open(in_shapefile).GetLayer()
# Filter by our query
query_str = '"{}" = "{}"'.format(filter_field, value)
input_layer.SetAttributeFilter(query_str)
# Copy Filtered Layer and Output File
driver = ogr.GetDriverByName('ESRI Shapefile')
out_ds = driver.CreateDataSource(out_shapefile)
out_layer = out_ds.CopyLayer(input_layer, str(value))
del input_layer, out_layer, out_ds
return out_shapefile
# for example
create_filtered_shapefile('highways', 'fclass', 'gis.osm_roads_free_1.shp')