Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMPAT: geopandas 1.0 compatibility #551

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions momepy/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import math

import geopandas as gpd
import networkx as nx
import numpy as np
import pandas as pd
import shapely
from packaging.version import Version
from tqdm.auto import tqdm # progress bar

from .utils import _azimuth
Expand All @@ -27,6 +29,8 @@
"Neighbors",
]

GPD_GE_013 = Version(gpd.__version__) >= Version("0.13.0")


class Orientation:
"""
Expand Down Expand Up @@ -134,7 +138,10 @@ class SharedWalls:
def __init__(self, gdf):
self.gdf = gdf

inp, res = gdf.sindex.query_bulk(gdf.geometry, predicate="intersects")
if GPD_GE_013:
inp, res = gdf.sindex.query(gdf.geometry, predicate="intersects")
else:
inp, res = gdf.sindex.query_bulk(gdf.geometry, predicate="intersects")
left = gdf.geometry.take(inp).reset_index(drop=True)
right = gdf.geometry.take(res).reset_index(drop=True)
intersections = left.intersection(right).length
Expand Down Expand Up @@ -689,7 +696,10 @@ def __init__(self, gdf):
self.gdf = gdf
self.orientation = gdf.geometry.apply(self._orient)

inp, res = gdf.sindex.query_bulk(gdf.geometry, predicate="intersects")
if GPD_GE_013:
inp, res = gdf.sindex.query(gdf.geometry, predicate="intersects")
else:
inp, res = gdf.sindex.query_bulk(gdf.geometry, predicate="intersects")
itself = inp == res
inp = inp[~itself]
res = res[~itself]
Expand Down
36 changes: 27 additions & 9 deletions momepy/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
import pandas as pd
import shapely
from packaging.version import Version
from scipy.spatial import Voronoi
from shapely.geometry.base import BaseGeometry
from shapely.ops import polygonize
Expand All @@ -24,6 +25,8 @@
"get_network_ratio",
]

GPD_GE_013 = Version(gpd.__version__) >= Version("0.13.0")


def buffered_limit(gdf, buffer=100):
"""
Expand Down Expand Up @@ -448,9 +451,14 @@ def _enclosed_tessellation(
enclosures["position"] = range(len(enclosures))

# determine which polygons should be split
inp, res = buildings.sindex.query_bulk(
enclosures.geometry, predicate="intersects"
)
if GPD_GE_013:
inp, res = buildings.sindex.query(
enclosures.geometry, predicate="intersects"
)
else:
inp, res = buildings.sindex.query_bulk(
enclosures.geometry, predicate="intersects"
)
unique, counts = np.unique(inp, return_counts=True)
splits = unique[counts > 1]
single = unique[counts == 1]
Expand Down Expand Up @@ -998,9 +1006,14 @@ def enclosures(
)
additional = pd.concat([gdf.geometry for gdf in additional_barriers])

inp, res = enclosures.sindex.query_bulk(
additional.geometry, predicate="intersects"
)
if GPD_GE_013:
inp, res = enclosures.sindex.query(
additional.geometry, predicate="intersects"
)
else:
inp, res = enclosures.sindex.query_bulk(
additional.geometry, predicate="intersects"
)
unique = np.unique(res)

new = []
Expand Down Expand Up @@ -1042,9 +1055,14 @@ def enclosures(
"`limit` requires a GeoDataFrame or GeoSeries with Polygon or "
"MultiPolygon geometry to be used with `clip=True`."
)
_, encl_index = final_enclosures.representative_point().sindex.query_bulk(
limit.geometry, predicate="contains"
)
if GPD_GE_013:
_, encl_index = final_enclosures.representative_point().sindex.query(
limit.geometry, predicate="contains"
)
else:
_, encl_index = final_enclosures.representative_point().sindex.query_bulk(
limit.geometry, predicate="contains"
)
keep = np.unique(encl_index)
return final_enclosures.iloc[keep]

Expand Down
14 changes: 11 additions & 3 deletions momepy/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np
import pandas as pd
import shapely
from packaging.version import Version
from scipy.signal import find_peaks
from scipy.stats import gaussian_kde
from shapely.geometry import LineString, Point
Expand All @@ -33,6 +34,8 @@
"FaceArtifacts",
]

GPD_GE_013 = Version(gpd.__version__) >= Version("0.13.0")


def preprocess(
buildings, size=30, compactness=0.2, islands=True, loops=2, verbose=True
Expand Down Expand Up @@ -855,9 +858,14 @@ def _selecting_incoming_lines(rab_multipolygons, edges, angle_threshold=0):
"""
# selecting the lines that are touching but not covered by
touching = gpd.sjoin(edges, rab_multipolygons, predicate="touches")
edges_idx, rabs_idx = rab_multipolygons.sindex.query_bulk(
edges.geometry, predicate="covered_by"
)
if GPD_GE_013:
edges_idx, _ = rab_multipolygons.sindex.query(
edges.geometry, predicate="covered_by"
)
else:
edges_idx, _ = rab_multipolygons.sindex.query_bulk(
edges.geometry, predicate="covered_by"
)
idx_drop = edges.index.take(edges_idx)
touching_idx = touching.index
ls = list(set(touching_idx) - set(idx_drop))
Expand Down
12 changes: 8 additions & 4 deletions momepy/tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import momepy as mm

GPD_GE_013 = Version(gpd.__version__) >= Version("0.13.0")

class TestElements:
def setup_method(self):
Expand Down Expand Up @@ -146,10 +147,13 @@ def test_Blocks_inner(self):
assert not blocks.tessellation_id.isna().any()
assert not blocks.buildings_id.isna().any()
assert len(blocks.blocks) == 9
assert (
len(blocks.blocks.sindex.query_bulk(blocks.blocks.geometry, "overlaps")[0])
== 0
)
if GPD_GE_013:
assert len(blocks.blocks.sindex.query(blocks.blocks.geometry, "overlaps")[0]) == 0
else:
assert (
len(blocks.blocks.sindex.query_bulk(blocks.blocks.geometry, "overlaps")[0])
== 0
)

def test_get_network_id(self):
buildings_id = mm.get_network_id(self.df_buildings, self.df_streets, "nID")
Expand Down