import osmnx as ox
import geopandas as gpd
import pystac_client
from pystac_client import Client
import planetary_computer
import os
import geojson
import numpy as np
import geopandas
import matplotlib.pyplot as plt
%matplotlib inline
from shapely import geometry
Building Footprints - OSM AND Microsoft
Notebook to download buildings data from osm open data and Microsoft Building Footprints. These are both open data sets to download building data that have varying degrees of accuracy across geographic regions. We also downloaded Open Africa buildings from Google, however, we used the website’s GUI to download instead of programatically:
- https://sites.research.google/open-buildings/#download
We have also created osm queries through overpass to gather the asset types relevant to the World Bank.
This query returns objects relevant to the World Bank which unfortunately doesn’t return many shapes for our AOI.
We can illustrate this query in New York where objects are returned. This may be due to more active OSM contributors in North America and speaks to volunteered geographic information systems inconsistent quality.
To gather landuse from our AOI.
For our area in Beitbridge we can query OSM from a bbox and through a place name query.
= [-22.148,-22.254,30.054,29.938] place_bbox
= "Zimbabwe" place_name
Additionally, we can save the Save AOI as a geojson for future use. lon_lat_list
is hard coded here.
= [[29.938,-22.254],[29.938,-22.148],[30.054,-22.148],[30.054,-22.254],[29.938,-22.254]]
lon_lat_list = geometry.Polygon(lon_lat_list)
polygon_geom = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon_geom])
polygon ='AOI_polygon.geojson', driver='GeoJSON') polygon.to_file(filename
OSM Buildings
Create a graph from OSM using place_bbox
for Beitbridge.
= ox.graph_from_bbox(place_bbox[0],place_bbox[1],place_bbox[2],place_bbox[3], network_type='drive', simplify=True, retain_all=False)
G = ox.plot_graph(G) fig, ax
Get the building footprints from a bbox and plot them.
#plot building footprint
= ox.geometries_from_bbox(place_bbox[0],place_bbox[1],place_bbox[2],place_bbox[3],tags={'building': True})
buildings = ox.plot_footprints(buildings, alpha=0.4, show=False)
fig, ax =ax, facecolor="blue", alpha=0.7, zorder=2)
buildings.plot(ax
plt.tight_layout()"off") plt.axis(
(29.938105, 30.0540557, -22.2513996, -22.1488729)
MS Buildings
Search against the Planetary Computer STAC API. You may need to register for an API key from planetary computer to make the following run:
= pystac_client.Client.open(
catalog "https://planetarycomputer.microsoft.com/api/stac/v1/",
=planetary_computer.sign_inplace,
modifier )
Define your search with CQL2 syntax
= catalog.search(filter_lang="cql2-json", filter={
search "op": "and",
"args": [
"op": "=", "args": [{"property": "collection"}, "ms-buildings"]},
{"op": "=", "args": [{"property": "msbuildings:region"}, place_name]},
{
{"op": ">=",
"args": [ { "property": "end_datetime" }, { "timestamp": "2018-11-20T00:00:00+00:00" } ]
}
] })
Grab the item from the search results
= next(search.get_items())
item print(item)
print(item.assets)
= item.assets["data"] asset
<Item id=Zimbabwe_2022-07-06>
{'data': <Asset href=abfs://footprints/global/2022-07-06/ml-buildings.parquet/RegionName=Zimbabwe>}
Convert data into geopandas
= geopandas.read_parquet(
df
asset.href,=asset.extra_fields["table:storage_options"],
storage_options
)=df.drop(columns="quadkey")
df=geopandas.GeoDataFrame(df, crs="EPSG:4326")
gdfprint(df.head())
ImportError: Install adlfs to access Azure Datalake Gen2 and Azure Blob Storage
Clip
Click our data to our AOI
# Clip the data using GeoPandas clip
= gpd.clip(gdf, polygon) MS_clip
=geopandas.GeoDataFrame(MS_clip, crs="EPSG:4326")
MS_Buildings
MS_Buildings.head()="ms_buildings.geojson", driver='GeoJSON') MS_Buildings.to_file(filename
Save OSM Buildings
Save the OSM Buildings as a geojson
=geopandas.GeoDataFrame(buildings, crs="EPSG:4326") OSM_Buildings
OSM_Buildings.head()= OSM_Buildings[['geometry','building','nodes']]
OSM_Buildings1 #OSM_Buildings.drop(columns=['osmid', 'layer', 'amenity', 'brand', 'name', 'shop', 'brand', 'roof:shape', 'ways', 'type'])
with open("./OSM_Buildings.geojson",mode="w") as f:
geojson.dump(OSM_Buildings,f)