easyidp.shp.read_shp

easyidp.shp.read_shp(shp_path, shp_proj=None, encoding='utf-8', return_proj=False)

读取shp文件到python numpy对象

参数:
  • shp_path (str) -- shp文件的路径

  • shp_proj (str | pyproj object) -- 默认值为None,将自动从与shp文件名相同的prj文件中读取,或者通过 read_shp(..., shp_proj=pyproj.CRS.from_epsg(4326), ...)read_shp(..., shp_proj=r'path/to/{shp_name}.prj', ...) 手动指定

  • encoding (str) -- 默认值为'utf-8',对于某些中文字符,可能需要'gbk'

  • return_proj (bool, optional) -- 默认值为 False,如果设置为 true,将返回当前 shp 文件的额外 pyproj.CRS 对象。

返回:

  • list[np.ndarray] -- Polygon coordinates for each shape in original order.

  • list[dict] -- Attribute records of each shape in original order.

  • dict -- Field map in format {"FIELD_NAME": int_id}.

    {'id1': np.array([[x1,y1],[x2,y2],...]),
     'id2': np.array([[x1,y1],[x2,y2],...]),...}
    
  • pyproj.CRS, 可选 -- 一旦设置 return_proj=True

示例

示例shp文件具有以下列:

[0] ID

[1] MASSIFID

[2] CROPTYPE

[3] CROPDATE

[4] CROPAREA

[5] ATTID

23010...0000

23010...0000

小麦

2018-09-01

61525.26302

23010...0012

23010...0012

蔬菜

2018-09-01

2802.33512

23010...0014

23010...0014

玉米

2018-09-01

6960.7745

23010...0061

23010...0061

牧草

2018-09-01

25349.08639

23010...0062

23010...0062

玉米

2018-09-01

71463.27666

...

...

...

...

...

...

23010...0582

23010...0582

胡萝卜

2018-09-01

288.23876

23010...0577

23010...0577

杂豆

2018-09-01

2001.80384

23010...0583

23010...0583

大豆

2018-09-01

380.41704

23010...0584

23010...0584

其它

2018-09-01

9133.25998

23010...0585

23010...0585

其它

2018-09-01

1704.27193

First, prepare data:

>>> import easyidp as idp
>>> testdata = idp.data.TestData()
>>> data_path = testdata.shp.complex_shp

Then read geometry and attributes:

>>> polygons, records, fields = idp.shp.read_shp(data_path, encoding='gbk')
>>> polygons[0]
array([[ 45.83319255, 126.84383445],
       [ 45.83222256, 126.84212197],
       ...,
       [ 45.83321205, 126.84381378],
       [ 45.83319255, 126.84383445]])
>>> records[0]
{'ID': '230104112201809010000000000', 'MASSIFID': '2301041120000000000', ...}
>>> fields
{'ID': 0, 'MASSIFID': 1, 'CROPTYPE': 2, ...}