我正在尝试将经纬度地理坐标列表映射到纽约市辖区。我有一个包含Polygons的.geojson文件,以及我的.csv数据集,其中包含纬度/经度作为两个单独的列。
如何检查点(.csv)是否在给定的多边形(.geojson)内,然后将要素(precinct)添加到熊猫数据框?
链接到geojson文件:https://github.com/veltman/wherewolf/blob/master/examples/node/police-precincts.geojson
导入json 导入csv 从shapely.geometry导入形状,点
uber_df = pd.read_csv('uber_preprocessed.csv')
将open('police_precincts.geojson')设为f: 数据= json.load(f)
# loop thru coordinates in the uber csv file
for lat in uber_df['Lat']:
pointLat = float(lat)
for lon in uber_df['Lon']:
pointLon = float(lon)
point = Point(pointLon, pointLat)
# loop thru features in geoJSON file
for feature in data['features']:
polygon = shape(feature["geometry"])
if polygon.contains(point):
# grab the data
newx = feature["properties"]["Precinct"]
# append the data
precincts = []
precincts.append(newx)
# write to file
with open('newfile.csv', 'wb') as newFile:
wr = csv.writer(newFile, quoting=csv.QUOTE_ALL)
newFile.writerow(precincts)
我知道最后一行代码正在写入.csv,但是我想将每个要素映射到数据集中的每个条目。我该怎么办?