【问题标题】:python OGR Shapefile export featurespython OGR Shapefile 导出功能
【发布时间】:2018-04-06 10:28:44
【问题描述】:

我已经下载了一个 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

关于如何做到这一点的任何建议?

【问题讨论】:

  • 什么是 fclass?一个属性?
  • shapefile 的布局如何?是每层一个特征,您想要迭代层还是想要迭代第 0 层中的特征?
  • fclass确实是一个属性,它包含了道路的类型(例如:高速公路、主干道、自行车道等)。 shapefile 只有一层,在该层中具有特征(道路线)。

标签: python gis openstreetmap gdal ogr


【解决方案1】:

试试这个。

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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2014-07-09
    • 2010-09-25
    • 2011-12-10
    • 1970-01-01
    • 2017-01-22
    相关资源
    最近更新 更多