marceloprates / prettymaps

A small set of Python functions to draw pretty maps from OpenStreetMap data. Based on osmnx, matplotlib and shapely libraries.
GNU Affero General Public License v3.0
11.12k stars 522 forks source link

Using custom bounding area for rendering #31

Open Jakub-L opened 3 years ago

Jakub-L commented 3 years ago

At the moment, as far as I am aware, there are 2 ways to define what to render:

  1. Provide an address or coordinates + a radius around that point
  2. Provide an enclosed feature in OSM (region, city, lake, etc.)

I would like to define the area to render by a number of coordinates - e.g. four street corners in a city.

I tried to do it with a Polygon, with a list of coordinates, but it errors out:

Traceback (most recent call last):
  File "/home/jakub-l/prettymap/main.py", line 9, in <module>
    backup = plot(
  File "/home/jakub-l/.pyenv/versions/prettymap/lib/python3.9/site-packages/prettymaps/draw.py", line 189, in plot
    layers = {
  File "/home/jakub-l/.pyenv/versions/prettymap/lib/python3.9/site-packages/prettymaps/draw.py", line 190, in <dictcomp>
    layer: get_layer(
  File "/home/jakub-l/.pyenv/versions/prettymap/lib/python3.9/site-packages/prettymaps/fetch.py", line 132, in get_layer
    return unary_union(ox.project_gdf(kwargs['perimeter']).geometry)
  File "/home/jakub-l/.pyenv/versions/prettymap/lib/python3.9/site-packages/osmnx/projection.py", line 72, in project_gdf
    if gdf.crs is None or len(gdf) < 1:
AttributeError: 'Polygon' object has no attribute 'crs'
G21-Goose commented 3 years ago

Hi, You can create a Polygon in a GeoDataFrame. Note that the Latitude and Longitude are in reverse order. This is an example of what it should look like for a box around Eureka California around (40.8003, -124.1628):

GeoDataFrame(geometry=[Polygon( ( (-124.1879,40.7762),(-124.1211,40.7737),(-124.1189,40.8251),(-124.1738,40.8194),(-124.1927,40.7868) ) )], crs=4326)

at the moment this might fail if you try this with streets, but I've just done a PR that should fix this #48.

b-a0 commented 1 month ago

Another point I ran into: the index should be a RangeIndex as otherwise it prettymaps cannot create a Nominatim query properly.