我正在尝试将数据绘制到地图上。我想为特定城市的地图上的特定点生成数据(例如,到一个或多个预定位置的运输时间)。
我在这里找到了纽约市的数据:https://data.cityofnewyork.us/City-Government/Borough-Boundaries/tqmj-j8zm
看起来他们有一个可用的形状文件。我想知道是否有一种方法可以在每个自治市的形状文件范围内对经纬度网格进行采样(也许使用匀称的包装等)。
很抱歉,如果这很幼稚,我对使用这些文件不是很熟悉-我正在做一个有趣的项目来了解它们
答案 0 :(得分:0)
我想出了办法。本质上,我只是创建了完整的点网格,然后删除了不属于自治市镇的形状文件中的那些点。这是代码:
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(',')]
我还得到了纬度-经度坐标矩阵,该矩阵用于为城市中每个点到哥伦比亚医学园区的运输时间图。这是那张地图: