lst*_*tbl 3 python shapefile geo
我正在尝试将数据绘制到地图上。我想为特定城市的地图上的特定点生成数据(例如到一个或多个预先指定位置的运输时间)。
我在这里找到了纽约市的数据:https : //data.cityofnewyork.us/City-Government/Borough-Boundaries/tqmj-j8zm
看起来他们有一个可用的 shapefile。我想知道是否有一种方法可以在每个行政区的 shapefile 范围内对经纬度网格进行采样(可能使用 Shapely 包等)。
对不起,如果这是天真,我不是很熟悉处理这些文件——我这样做是作为一个有趣的项目来了解它们
我想出了如何做到这一点。本质上,我只是创建了一个完整的点网格,然后删除了那些不在与行政区对应的形状文件内的点。这是代码:
import geopandas
from geopandas import GeoDataFrame, GeoSeries
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
import matplotlib.cm as cm
%matplotlib inline
import seaborn as sns
from shapely.geometry import Point, Polygon
import numpy as np
import googlemaps
from datetime import datetime
plt.rcParams["figure.figsize"] = [8,6]
# Get the shape-file for NYC
boros = GeoDataFrame.from_file('./Borough Boundaries/geo_export_b641af01-6163-4293-8b3b-e17ca659ed08.shp')
boros = boros.set_index('boro_code')
boros = boros.sort_index()
# Plot and color by borough
boros.plot(column = 'boro_name')
# Get rid of are that you aren't interested in (too far away)
plt.gca().set_xlim([-74.05, -73.85])
plt.gca().set_ylim([40.65, 40.9])
# make a grid of latitude-longitude values
xmin, xmax, ymin, ymax = -74.05, -73.85, 40.65, 40.9
xx, yy = np.meshgrid(np.linspace(xmin,xmax,100), np.linspace(ymin,ymax,100))
xc = xx.flatten()
yc = yy.flatten()
# Now convert these points to geo-data
pts = GeoSeries([Point(x, y) for x, y in zip(xc, yc)])
in_map = np.array([pts.within(geom) for geom in boros.geometry]).sum(axis=0)
pts = GeoSeries([val for pos,val in enumerate(pts) if in_map[pos]])
# Plot to make sure it makes sense:
pts.plot(markersize=1)
# Now get the lat-long coordinates in a dataframe
coords = []
for n, point in enumerate(pts):
coords += [','.join(__ for __ in _.strip().split(' ')[::-1]) for _ in str(point).split('(')[1].split(')')[0].split(',')]
Run Code Online (Sandbox Code Playgroud)
我还得到了一个经纬度坐标矩阵,用于制作城市中每个点到哥伦比亚医学校园的运输时间地图。这是那张地图:
| 归档时间: |
|
| 查看次数: |
6105 次 |
| 最近记录: |