Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_nearest_node() had [y(lat),x(lon)], now nearest_nodes() has x(lon… #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions scripts/02.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,27 @@
G_carall = G_caralls_simplified[placeid]

for poiid, poitag in poiparameters.items():
gdf = ox.geometries.geometries_from_polygon(location, poitag)
gdf = gdf[gdf['geometry'].type == "Point"] # only consider points, no polygons etc
# Now snap to closest nodes in street network, save the nearest node ids
nnids = set()
for g in gdf['geometry']:
n = ox.distance.get_nearest_node(G_carall, [g.y, g.x])
if not n in nnids and haversine((g.y, g.x), (G_carall.nodes[n]["y"], G_carall.nodes[n]["x"]), unit="m") <= snapthreshold:
nnids.add(n)
# nnids = ox.distance.get_nearest_nodes(G_carall, gdf['geometry'].x, gdf['geometry'].y) # This could be faster but does not seem to work correctly
with open(PATH["data"] + placeid + "/" + placeid + '_' + 'poi_' + poiid + '_nnidscarall.csv', 'w') as f:
for item in nnids:
f.write("%s\n" % item)

gdf = gdf.apply(lambda c: c.astype(str) if c.name != 'geometry' else c, axis=0)
try: # For some cities writing the gdf does not work (i.e. London, Manhattan)
gdf.to_file(PATH["data"] + placeid + "/" + placeid + '_' + 'poi_' + poiid + '.gpkg', driver = 'GPKG')
try:
gdf = ox.geometries.geometries_from_polygon(location, poitag)
gdf = gdf[gdf['geometry'].type == "Point"] # only consider points, no polygons etc
# Now snap to closest nodes in street network, save the nearest node ids
nnids = set()
for g in gdf['geometry']:
n = ox.distance.nearest_nodes(G_carall, g.x, g.y) # !! get_nearest_node() had [y(lat),x(lon)], now nearest_nodes() has x(lon),y(lat)
if not n in nnids and haversine((g.y, g.x), (G_carall.nodes[n]["y"], G_carall.nodes[n]["x"]), unit="m") <= snapthreshold:
nnids.add(n)
with open(PATH["data"] + placeid + "/" + placeid + '_' + 'poi_' + poiid + '_nnidscarall.csv', 'w') as f:
for item in nnids:
f.write("%s\n" % item)

gdf = gdf.apply(lambda c: c.astype(str) if c.name != 'geometry' else c, axis=0)
try: # For some cities writing the gdf does not work (i.e. London, Manhattan)
gdf.to_file(PATH["data"] + placeid + "/" + placeid + '_' + 'poi_' + poiid + '.gpkg', driver = 'GPKG')
except:
print("Notice: Writing the gdf did not work for " + placeid)
if debug: gdf.plot(color = 'red')
except:
print("Notice: Writing the gdf did not work for " + placeid)
if debug: gdf.plot(color = 'red')
print("No stations in " + placeinfo["name"] + ". No POIs created.")


for placeid, placeinfo in tqdm(cities.items(), desc = "Cities"):
Expand All @@ -60,6 +62,7 @@
location = locations[placeid]



# FIRST, determine the most common bearing, for the best grid orientation
G = G_caralls[placeid]
bearings = {}
Expand Down Expand Up @@ -148,7 +151,7 @@
# 3) Snap grid points to map
nnids = set()
for g in gridpoints_cut:
n = ox.distance.get_nearest_node(G, [g[1], g[0]])
n = ox.distance.nearest_nodes(G, g[0], g[1]) # !! get_nearest_node() had [y(lat),x(lon)], now nearest_nodes() has x(lon),y(lat)
if n not in nnids and haversine((g[1], g[0]), (G.nodes[n]["y"], G.nodes[n]["x"]), unit="m") <= snapthreshold:
nnids.add(n)
with open(PATH["data"] + placeid + "/" + placeid + '_poi_grid_nnidscarall.csv', 'w') as f:
Expand Down