Python:使用shapefile

时间:2017-12-22 15:51:44

标签: python shapefile fiona

我必须对来自新加坡的shapefile做一些分析。

特别是我必须将人口普查数据中的居民与地区进行比较。 首先,我打开shapefile,然后保存名称

import fiona
multipol = fiona.open("shapefile/MP98_PLNG_AREA_NO_SEA_PL.shp")

NAME = []
for i in range(0, len(multipol)):
    multi = multipol[i] # only one feature in the shapefile
    tmp = multi.items()
    tmp1 = tmp[3][1].items()
    name  = tmp1[2]
    nameOK = str(name[1])
    NAME.append(nameOK)

然后我打开人口普查数据的.csv文件

cp = pd.read_csv('popCensus.csv')
cp.head(5)
    name                  population
0   Ang Mo Kio              166820
1   Bedok                   284930
2   Bishan                  90280
3   Boon Lay & Pianeer      150
4   Bukit Batok             138290

最后,我有一个包含相同单元格的文件,其中包含这些单元格的坐标

cellCoord = pd.read_csv('cells_newids.csv');
cellCoord.head(5)
    ID  lon         lat         idI   idJ
0   0   103.619740  1.280485    11    35
1   1   103.622632  1.268944    12    31
2   2   103.622632  1.274714    12    33
3   3   103.622632  1.277600    12    34
4   4   103.622632  1.280485    12    35

对于每个单元格,我想检查它们是shapefile的哪个多边形,并为它们指定名称和相应的居民数量。

我想做类似的事情,但我不知道如何制作循环

from shapely.geometry import Polygon, Point, MultiPolygon

for i in cellCoord.index:
    y = cellCoord.lat[i]
    x = cellCoord.lon[i]

    point = Point(x, y)
    for j in range(0, len(multipol)):
        multi = multipol[j]
        poly = Polygon(multi)
        if poly.contains(point):
            tmp = multi.items()
            tmp1 = tmp[3][1].items()
            name  = tmp1[2]
            nameOK = str(name[1])
            cellCoord['name'][i] = nameOK

但它不起作用,因为我无法从Polygon创建一个有价值的multi,这是dictionary,如下所示。

multi

{'geometry': {'coordinates': [[(31098.01370000001, 43946.6054999996),
    (31096.58990000002, 43910.996099999174),
    (31096.115199999884, 43879.1875),
    (31097.539099999703, 43831.710999999195),
    (31098.488300000317, 43800.375),
    (31097.539099999703, 43768.5625),
    (31094.150399999693, 43735.6445000004),
    (31091.427699999884, 43717.42190000042),
    (31085.1464999998, 43684.3320000004),
    (31076.9785000002, 43651.664100000635)]],
  'type': 'Polygon'},
 'id': '53',
 'properties': OrderedDict([(u'OBJECTID', 54L),
              (u'PLN_AREA_C', u'YS'),
              (u'PLN_AREA_N', u'YISHUN'),
              (u'INC_CRC', u'BDD68D30F2B1F5C3'),
              (u'FMEL_UPD_D', '2016-04-18'),
              (u'X_ADDR', 28486.6553),
              (u'Y_ADDR', 44142.7874),
              (u'SHAPE_Leng', 20810.5678749),
              (u'SHAPE_Area', 21688951.6662)]),
 'type': 'Feature'}

0 个答案:

没有答案