Generating building footprints from street point - python

I am using OSMNX to generate building footprints from a specific point. However, OSMNx doesn't display any footprints. I verified the same place from GoogleMaps where it shows there are many buildings around set point as shown in the below figure:
OSMNx Script
import osmnx
import pandas as pd
import networkx as nx
import osmnx as ox
from IPython.display import Image
import matplotlib.colors as colors
import pprint
ox.config(use_cache=True, log_console=True)
# get a graph for some city
G = ox.graph_from_place('Colac, Victoria, Australia', network_type='drive')
img_folder = 'images'
extension = 'png'
size = 640
dpi = 20
# #-----------------------------------------------------------------------------------------------------
def make_plot(place, point, network_type='drive', bldg_color='orange', dpi=400,
dist=500, default_width=4, street_widths=None):
gdf = ox.footprints.footprints_from_point(point=point, distance=dist)
fig, ax = ox.plot_figure_ground(point=point, dist=dist, network_type=network_type, default_width=default_width,
street_widths=street_widths, save=False, show=False, close=True)
fig, ax = ox.footprints.plot_footprints(gdf, fig=fig, ax=ax, color=bldg_color, set_bounds=False,
save=True, show=False, close=True, filename=place, dpi=dpi)
# #-----------------------------------------------------------------------------------------------------
place = 'Colac_buildings'
#Colac Lat,Lon
point = (-38.338850, 143.599013)
make_plot(place, point)
Image('{}/{}.{}'.format(img_folder, place, extension), height=size, width=size)
Does it mean the information is missing in OSMNx? What is the best way to get the building information or footprints?
Actually I am trying to extract addresses of all buildings in a specific area. Is there any other way to achieve this?

OSMNx uses data from OpenStreetMap, not from Google Maps. For your location -38.338850, 143.599013 there are no buildings in OSM yet (but there are several nearby, e.g. to the west).
To improve this you can create an OSM account and start to improve the map. Besides building footprints you can also add missing addresses to OSM (if you have access to a compatible license). Afterwards you can retrieve them with a geocoder, for example Nominatim.

Related

Fill U.S. counties by value using Python & Cartopy?

I'd like to know how to fill in a map of U.S. counties by value (i.e., a chloropleth map), using Python 3 and Cartopy, and I haven't yet found anything online to guide me in that. That filled value could be, for instance, highest recorded tornado rating (with counties left blank for no recorded tornadoes), or even something arbitrary such as whether I've visited (=1) or lived (=2) in the county. I found a helpful MetPy example to get the county boundaries on a map:
https://unidata.github.io/MetPy/latest/examples/plots/US_Counties.html
What I envision is somehow setting a list (or dictionary?) of county names to a certain value, and then each value would be assigned to a particular fill color. This is my current script, which generates a nice blank county map of the CONUS/lower 48 (though I'd eventually also like to add Alaska/Hawaii insets).
import cartopy
import cartopy.crs as ccrs
import matplotlib as mpl
import matplotlib.pyplot as plt
from metpy.plots import USCOUNTIES
plot_type = 'png'
borders = cartopy.feature.BORDERS
states = cartopy.feature.NaturalEarthFeature(category='cultural', scale='10m', facecolor='none', name='admin_1_states_provinces_lakes')
oceans = cartopy.feature.OCEAN
lakes = cartopy.feature.LAKES
mpl.rcParams['figure.figsize'] = (12,10)
water_color = 'lightblue'
fig = plt.figure()
ax = plt.axes(projection=ccrs.LambertConformal(central_longitude=-97.5, central_latitude=38.5, standard_parallels=(38.5,38.5)))
ax.set_extent([-120, -74, 23, 50], ccrs.Geodetic())
ax.coastlines()
ax.add_feature(borders, linestyle='-')
ax.add_feature(states, linewidth=0.50, edgecolor='black')
ax.add_feature(oceans, facecolor=water_color)
ax.add_feature(lakes, facecolor=water_color, linewidth=0.50, edgecolor='black')
ax.add_feature(USCOUNTIES.with_scale('500k'), linewidth=0.10, edgecolor='black')
plt.savefig('./county_map.'+plot_type)
plt.close()
Any ideas or tips on how to assign values to counties and fill them accordingly?
So Cartopy's shapereader.Reader can give you access to all of the records in the shapefile, including their attributes. Putting this together with MetPy's get_test_data to get access to the underlying shapefile you can get what you want, assuming you have a dataset that maps e.g. FIPSCODE to EF rating:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
cmap = plt.get_cmap('magma')
norm = plt.Normalize(0, 5)
# Fake tornado dataset with a value for each county code
tor_data = dict()
# This will only work (have access to the shapefile's database of
# attributes after it's been download by using `USCOUNTIES` or
# running get_test_data() for the .shx and .dbf files as well.
for rec in shpreader.Reader(get_test_data('us_counties_20m.shp',
as_file_obj=False)).records():
# Mimic getting data, but actually getting a random number
# GEOID seems to be the FIPS code
max_ef = tor_data.get(rec.attributes['GEOID'], np.random.randint(0, 5))
# Normalize the data to [0, 1] and colormap manually
color = tuple(cmap(norm(max_ef)))
# Add the geometry to the plot, being sure to specify the coordinate system
ax.add_geometries([rec.geometry], crs=ccrs.PlateCarree(), facecolor=color)
ax.set_extent((-125, -65, 25, 48))
That gives me:
I'm not sure about passing in a dict, but you can pass in a list to facecolor.
ax.add_feature(USCOUNTIES.with_scale('500k'), linewidth=0.10, edgecolor='black', facecolor=["red", "blue", "green"])
If you know how many counties there are you can make a list that long by:
import matplotlib.cm as cm
import numpy as np
number_of_counties = 3000
color_scale = list(cm.rainbow(np.linspace(0, 1, number_of_counties)))
ax.add_feature(USCOUNTIES.with_scale('500k'), linewidth=.10, edgecolor="black", facecolor=color_scale)
but they didn't make it easy to extract the names from USCOUNTIES. You can see where it is defined in your source code:
from metpy import plots
print(plots.__file__)
If you go inside the directory printed there is a file named cartopy_utils.py and inside the class definition for class MetPyMapFeature(Feature): you will see USCOUNTIES. You might have better luck than I did mapping county names to the geometric shapes.
EDIT: Also, I just used cm.rainbow as an example, you can choose from any color map https://matplotlib.org/stable/tutorials/colors/colormaps.html. Not sure if it even goes up to 3000, but you get the idea.

Excluding points within list of buildings' shapes in Python

I have a dataframe that contains thousands of points with geolocation (longitude, latitude) for Washington D.C. The following is a snippet of it:
import pandas as pd
df = pd.DataFrame({'lat': [ 38.897221,38.888100,38.915390,38.895100,38.895100,38.901005,38.960491,38.996342,38.915310,38.936820], 'lng': [-77.031048,-76.898480,-77.021380,-77.036700,-77.036700 ,-76.990784,-76.862907,-77.028131,-77.010403,-77.184930]})
If you plot the points in the map you can see that some of them are clearly within some buildings:
import folium
wash_map = folium.Map(location=[38.8977, -77.0365], zoom_start=10)
for index,location_info in df.iterrows():
folium.CircleMarker(
location=[location_info["lat"], location_info["lng"]], radius=5,
fill=True, fill_color='red',).add_to(wash_map)
wash_map.save('example_stack.html')
import webbrowser
import os
webbrowser.open('file://'+os.path.realpath('example_stack.html'), new=2)
My goal is to exclude all the points that are within buildings. For that, I first download bounding boxes for the city buildings and then try to exclude points within those polygons as follows:
import osmnx as ox
#brew install spatialindex this solves problems in mac
%matplotlib inline
ox.config(log_console=True)
ox.__version__
tags = {"building": True}
gdf = ox.geometries.geometries_from_point([38.8977, -77.0365], tags, dist=1000)
gdf.shape
For computational simplicity I have requested the shapes of all buildings around the White house with a radius of 1 km. On my own code I have tried with bigger radiuses to make sure all the buildings are included.
In order to exclude points within the polygons I developed the following function (which includes the shape obtention):
def buildings(df,center_point,dist):
import osmnx as ox
#brew install spatialindex this solves problems in mac
%matplotlib inline
ox.config(log_console=True)
ox.__version__
tags = {"building": True}
gdf = ox.geometries.geometries_from_point(center_point, tags,dist)
from shapely.geometry import Point,Polygon
# Next step is to put our coordinates in the correct shapely format: remember to run the map funciton before
#df['within_building']=[]
for point in range(len(df)):
if gdf.geometry.contains(Point(df.lat[point],df.lng[point])).all()==False:
df['within_building']=False
else :
df['within_building']=True
buildings(df,[38.8977, -77.0365],1000)
df['within_building'].all()==False
The function always returns that points are outside building shapes although you can clearly see in the map that some of them are within. I don't know how to plot the shapes over my map so I am not sure if my polygons are correct but for the coordinates they appear to be so. Any ideas?
The example points you provided don't seem to fall within those buildings' footprints. I don't know what your points' coordinate reference system is, so I guessed EPSG4326. But to answer your question, here's how you would exclude them, resulting in gdf_points_not_in_bldgs:
import geopandas as gpd
import matplotlib.pyplot as plt
import osmnx as ox
import pandas as pd
# the coordinates you provided
df = pd.DataFrame({'lat': [38.897221,38.888100,38.915390,38.895100,38.895100,38.901005,38.960491,38.996342,38.915310,38.936820],
'lng': [-77.031048,-76.898480,-77.021380,-77.036700,-77.036700 ,-76.990784,-76.862907,-77.028131,-77.010403,-77.184930]})
# create GeoDataFrame of point geometries
geom = gpd.points_from_xy(df['lng'], df['lat'])
gdf_points = gpd.GeoDataFrame(geometry=geom, crs='epsg:4326')
# get building footprints
tags = {"building": True}
gdf_bldgs = ox.geometries_from_point([38.8977, -77.0365], tags, dist=1000)
gdf_bldgs.shape
# get all points that are not within a building footprint
mask = gdf_points.within(gdf_bldgs.unary_union)
gdf_points_not_in_bldgs = gdf_points[~mask]
print(gdf_points_not_in_bldgs.shape) # (10, 1)
# plot buildings and points
ax = gdf_bldgs.plot()
ax = gdf_points.plot(ax=ax, c='r')
plt.show()
# zoom in to see better
ax = gdf_bldgs.plot()
ax = gdf_points.plot(ax=ax, c='r')
ax.set_xlim(-77.04, -77.03)
ax.set_ylim(38.89, 38.90)
plt.show()

Insert a geovoronoi library polygon to folium map in Python

To create a Voronoi polygon with geovoronoi lib i use:
polyShapes, puntos = voronoi_regions_from_coords(coords, milagroShape)
coords is a geoDataFrame object that it contains map´s locations and milagroShape is a polygon.shp. Now, to plot the Voronoi use the code:
fig, ax = subplot_for_map(figsize=[14, 8])
plot_voronoi_polys_with_points_in_area(ax, milagroShape, polyShapes, coords, puntos)
ax.set_title('Diagrama de Voronoi')
plt.tight_layout()
plt.show()
Now it works, the graph is showed on screen, but it´s only a mathplotlib plot.
I guess that I have to convert it into a geodataframe object (to that, I use geopandas library).
This is the map where I need to put the Voronoi graph:
Only the polygon of the city´s area is set, but I want to set the Voronoi too.
To add the city´s area I used the code below:
for _, r in milagro.iterrows(): #milagro is a geodataframe object
#sim_geo = gpd.GeoSeries(r['geometry'])
sim_geo = gpd.GeoSeries(r['geometry']).simplify(tolerance=0.0001)
geo_j = sim_geo.to_json()
geo_j = folium.GeoJson(data=geo_j,
style_function=lambda x: {'fillColor': 'orange'})
#folium.Popup(r['Name']).add_to(geo_j)
geo_j.add_to(mapaMilagro) #mapaMilagro is a folium map object
Libraries that i use for my proyect are:
import folium #map library
import pandas as pd #Data Frame
import matplotlib.pyplot as plt #to plot graphs
import condacolab #To install some libraries
import geopandas as gpd #Geo Data Frame library
from shapely.ops import cascaded_union #I don´t know what is this xd
from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area
from geovoronoi import voronoi_regions_from_coords, points_to_coords
polyShapes, puntos = voronoi_regions_from_coords(coords, milagroShape)
polyShapes is a dict where the keys are meaningless (?) numbers and the values are shapely polygons. You can load those into a new gpd dataframe.

Geopandas defaulting to world map despite only using US data and shape file

I've been trying to map the route of US domestic flights over a map of the United States through Geopandas. I can map my shapefile of flights without any problem, but when I try to add another layer showing the United States underneath, the resulting plot is the size of Geopandas' world map, but only shows the US and flights geometries.
import matplotlib as mp
import geopandas as gp
import numpy as np
import pandas as pd
import pyproj as pj
import matplotlib.pyplot as plt
import descartes
flights = gp.read_file(r"C:\Users\corne\git\flight\flights.shp")
usmap = gp.read_file(r"C:\Users\corne\git\flight\Igismap\UnitedStates_Boundary.shp")
usmap = usmap.to_crs({'init': 'epsg:4326'})
fig, ax = plt.subplots(figsize = (12,6))
flights.plot(ax=ax)
usmap.plot(ax = ax)
Here is the resulting map
US-only data being plotted on World map
I cant see any base map in your piece of code.
In javascript I use .shp .dbf .prj and Compiled shape all 4 files together. You would find all 4 files in the compressed form when you downloaded .shp from igismap.

Street names in OSMNX network maps

I am constructing street networks on osmnx using below code.I see that I can print lat/lon information, but
Is there a way to include street/road names in network maps as well? I don't see how to do this in the documentation. Thanks!
import osmnx as ox
G = ox.graph_from_bbox(37.79, 37.78, -122.41, -122.43, network_type='drive')
G_projected = ox.project_graph(G)
ox.plot_graph(G_projected)
Output:
Here's how you annotate your map with OSMnx to show street/road names (or any other edge attributes in the plot). The same logic would apply to labeling nodes instead.
import matplotlib.pyplot as plt
import osmnx as ox
ox.config(use_cache=True, log_console=True)
G = ox.graph_from_address('Piedmont, CA, USA', dist=200, network_type='drive')
G = ox.get_undirected(G)
fig, ax = ox.plot_graph(G, bgcolor='k', edge_linewidth=3, node_size=0,
show=False, close=False)
for _, edge in ox.graph_to_gdfs(G, nodes=False).fillna('').iterrows():
c = edge['geometry'].centroid
text = edge['name']
ax.annotate(text, (c.x, c.y), c='w')
plt.show()
The only aesthetic challenge being the label placement problem, which is one of the most difficult problems in computational cartography.

Categories

Resources