momepy icon indicating copy to clipboard operation
momepy copied to clipboard

StreetProfile shall warn if CRS is geographic

Open adhamenaya opened this issue 2 years ago • 5 comments

Describe the problem

I am following the sample source code on this link: Simplified detection of urban types . And after updating GeoPandas (0.11.0) and GDAL (3.5.0) packages, I started getting this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 profile = momepy.StreetProfile(streets, buildings)

File ~/opt/anaconda3/envs/PythonEnv/lib/python3.9/site-packages/momepy/dimension.py:590, in StreetProfile.__init__(self, left, right, heights, distance, tick_length, verbose)
    587         ticks.append([line_end_1, pt])
    588         ticks.append([line_end_2, pt])
--> 590 ticks = pygeos.linestrings(ticks)
    592 inp, res = right.sindex.query_bulk(ticks, predicate="intersects")
    593 intersections = pygeos.intersection(ticks[inp], right.geometry.values.data[res])

File ~/opt/anaconda3/envs/PythonEnv/lib/python3.9/site-packages/pygeos/decorators.py:80, in multithreading_enabled.<locals>.wrapped(*args, **kwargs)
     78     for arr in array_args:
     79         arr.flags.writeable = False
---> 80     return func(*args, **kwargs)
     81 finally:
     82     for arr, old_flag in zip(array_args, old_flags):

File ~/opt/anaconda3/envs/PythonEnv/lib/python3.9/site-packages/pygeos/creation.py:119, in linestrings(coords, y, z, indices, out, **kwargs)
    117 coords = _xyz_to_coords(coords, y, z)
    118 if indices is None:
--> 119     return lib.linestrings(coords, out=out, **kwargs)
    120 else:
    121     return simple_geometries_1d(coords, indices, GeometryType.LINESTRING, out=out)

ValueError: linestrings: Input operand 0 does not have enough dimensions (has 1, gufunc core with signature (i, d)->() requires 2)

Steps to reproduce

import geopandas
import libpysal
import momepy
import osmnx
import pandas

place = 'Znojmo, Czechia'

buildings = osmnx.geometries.geometries_from_place(place, tags={'building':True})

osm_graph = osmnx.graph_from_place(place, network_type='drive')
#osm_graph = osmnx.projection.project_graph(osm_graph, to_crs=local_crs)
streets = osmnx.graph_to_gdfs(
    osm_graph, 
    nodes=False, 
    edges=True,
    node_geometry=False, 
    fill_edge_geometry=True
)

profile = momepy.StreetProfile(streets, buildings)

Versions of your packages

GeoPandas=0.11.0 GDAL=3.5.0 momepy=0.5.3 libpysal=4.6.2 osmnx=1.2.1

Your operating system

macOS Monterey 12.4

Additional context

No response

adhamenaya avatar Jul 07 '22 13:07 adhamenaya

Thanks for the report. I see the same behaviour on my side. Not sure what is causing it though, will need to dig.

martinfleis avatar Jul 07 '22 18:07 martinfleis

Thanks for the report. I see the same behaviour on my side. Not sure what is causing it though, will need to dig.

Thanks @martinfleis - I need to work on this library for my uni research as soon as possible. Could you please advise what version of GeoPandas and GDAL is compatible with this library?

adhamenaya avatar Jul 13 '22 10:07 adhamenaya

@adhamenaya I figured out the issue. It is not a bug in momepy or any of the dependencies but in your code above.

I didn't notice that you commented out osmnx.projection.project_graph. Without this step, your geometry is in EPSG:4326, which means that the units are degrees. Since we create perpendicular lines along each segment in StreetProfile every 10 units by default, those 10 units are in your case 10 degrees. As a result, no lines is created. The code expects meters (or feet), so you need to project your data before using almost any function in momepy.

What we can do here on the momepy side is to raise a warning if the GeoDataFrame is in geographic CRS as geopandas does with buffer and similar and raise an informative error if the resulting set of perpendicular lines is empty. But those are just user convenience messages, there is no bug to fix.

martinfleis avatar Jul 13 '22 20:07 martinfleis

@martinfleis Thanks a lot for the clarification, and sorry about my misuse of library functions.

adhamenaya avatar Jul 14 '22 12:07 adhamenaya

I'll keep it open to add those warnings.

martinfleis avatar Jul 14 '22 13:07 martinfleis