2018-09-09 06:32:12 -04:00
|
|
|
"""Download and load a small open dataset for testing
|
2018-09-25 15:46:16 -04:00
|
|
|
|
|
|
|
Run this to create a CSV of buildings geometries.
|
|
|
|
|
|
|
|
Then run:
|
|
|
|
- load_geometries.sh (loading geometries to the database)
|
|
|
|
- create_buildings.sh (creating empty building records for each geometry)
|
2018-09-09 06:32:12 -04:00
|
|
|
"""
|
2018-09-10 05:44:09 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-09-09 06:32:12 -04:00
|
|
|
import os
|
2018-09-25 17:01:09 -04:00
|
|
|
import subprocess
|
|
|
|
|
2018-09-09 06:32:12 -04:00
|
|
|
import osmnx
|
|
|
|
|
|
|
|
# configure logging/caching
|
|
|
|
osmnx.config(log_console=True, use_cache=True)
|
|
|
|
|
|
|
|
# configure the image display
|
|
|
|
size = 256
|
|
|
|
|
|
|
|
# load buildings from about 1.5km² around UCL
|
|
|
|
point = (51.524498, -0.133874)
|
|
|
|
dist = 612
|
2020-06-18 05:31:34 -04:00
|
|
|
gdf = osmnx.footprints_from_point(point=point, dist=dist)
|
2018-09-09 06:32:12 -04:00
|
|
|
|
|
|
|
# preview image
|
2020-06-18 05:31:34 -04:00
|
|
|
gdf_proj = osmnx.projection.project_gdf(gdf, to_crs={'init': 'epsg:3857'})
|
2022-03-18 12:18:44 -04:00
|
|
|
gdf_proj = gdf_proj[gdf_proj.geometry.apply(lambda g: g.geom_type != 'MultiPolygon')] # noqa
|
2020-06-18 05:31:34 -04:00
|
|
|
|
2022-03-18 12:18:44 -04:00
|
|
|
fig, ax = osmnx.plot_footprints(gdf_proj, bgcolor='#333333',
|
|
|
|
color='w', figsize=(4, 4),
|
|
|
|
save=True, show=False, close=True,
|
|
|
|
filename='test_buildings_preview', dpi=600)
|
2018-09-09 06:32:12 -04:00
|
|
|
|
2018-09-25 15:46:16 -04:00
|
|
|
# save
|
2018-09-25 17:01:09 -04:00
|
|
|
test_dir = os.path.dirname(__file__)
|
|
|
|
test_data_geojson = str(os.path.join(test_dir, 'test_buildings.geojson'))
|
|
|
|
subprocess.run(["rm", test_data_geojson])
|
2018-09-09 06:32:12 -04:00
|
|
|
|
2018-09-10 05:44:09 -04:00
|
|
|
gdf_to_save = gdf_proj.reset_index(
|
2018-09-09 06:32:12 -04:00
|
|
|
)[
|
|
|
|
['index', 'geometry']
|
|
|
|
]
|
|
|
|
|
|
|
|
gdf_to_save.rename(
|
|
|
|
columns={'index': 'fid'}
|
|
|
|
).to_file(
|
2018-09-25 17:01:09 -04:00
|
|
|
test_data_geojson, driver='GeoJSON'
|
2018-09-09 06:32:12 -04:00
|
|
|
)
|
2018-09-25 17:01:09 -04:00
|
|
|
|
|
|
|
# convert to CSV
|
2018-09-30 16:23:19 -04:00
|
|
|
test_data_csv = str(os.path.join(test_dir, 'test_buildings.3857.csv'))
|
2018-09-25 17:01:09 -04:00
|
|
|
subprocess.run(["rm", test_data_csv])
|
2022-03-18 12:18:44 -04:00
|
|
|
subprocess.run(
|
|
|
|
["ogr2ogr", "-f", "CSV", test_data_csv,
|
|
|
|
test_data_geojson, "-lco", "GEOMETRY=AS_WKT"]
|
|
|
|
)
|
2018-09-25 17:01:09 -04:00
|
|
|
|
|
|
|
# add SRID for ease of loading to PostgreSQL
|
2022-03-18 12:18:44 -04:00
|
|
|
subprocess.run(
|
|
|
|
["sed", "-i", "s/^\"POLYGON/\"SRID=3857;POLYGON/",
|
|
|
|
test_data_csv]
|
|
|
|
)
|