easyidp.shp.read_shp

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

read shp file to python numpy object

Parameters:
  • shp_path (str) – the file path of *.shp

  • shp_proj (str | pyproj object) – by default None, will read automatically from prj file with the same name of shp filename, or give manually by read_shp(..., shp_proj=pyproj.CRS.from_epsg(4326), ...) or read_shp(..., shp_proj=r'path/to/{shp_name}.prj', ...)

  • encoding (str) – by default ‘utf-8’, for some chinese characters, ‘gbk’ may required

  • return_proj (bool, optional) – by default False, if given as true, will return extra pyproj.CRS object of current shp file.

Returns:

  • 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, optional – once set return_proj=True

Example

The example shp file has the following columns:

[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, ...}