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

Implement QgsGeometry.as_numpy and .as_shapely #58589

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

merydian
Copy link
Contributor

@merydian merydian commented Sep 5, 2024

Description

Adds methods to QgsGeometry to convert it to a (list of) numpy array(s) or shapely object(s). This is part of qgis/QGIS-Enhancement-Proposals#227. This enhancement improves integration with NumPy and Shapely while keeping them an optional dependency.

@github-actions github-actions bot added this to the 3.40.0 milestone Sep 5, 2024
Copy link

github-actions bot commented Sep 5, 2024

🪟 Windows builds ready!

Windows builds of this PR are available for testing here. Debug symbols for this build are available here.

(Built from commit f60bf29)

@@ -540,6 +540,7 @@ QgsException.__doc__ = "Defines a QGIS exception class."

try:
import numpy as _numpy
import shapely as _shapely
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shapely related things should be put in another try block, so that the raster part can work only if numpy is available

@roya0045
Copy link
Contributor

roya0045 commented Sep 6, 2024

What about geometries with Z/M coordinates?

By utilizing QgsGeometry.asWkb() this is ~8.45 times faster than the previous implementation.
def _geometry_as_shapely(self) -> _shapely.geometry.base.BaseGeometry:
wkb_qbytearray = self.asWkb() # Get the geometry in WKB format (QByteArray)
wkb_bytes = bytes(wkb_qbytearray)
shapely_geom = _shapely.from_wkb(wkb_bytes)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? It would save a copy of the data, and might squeeze out a bit more performance:

Suggested change
shapely_geom = _shapely.from_wkb(wkb_bytes)
shapely_geom = _shapely.from_wkb(wkb_qbytearray.data())

@nyalldawson nyalldawson added the Freeze Exempt Feature Freeze exemption granted label Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Freeze Exempt Feature Freeze exemption granted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants