-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Implement QgsGeometry.as_numpy and .as_shapely #58589
Conversation
python/PyQt6/core/__init__.py.in
Outdated
@@ -540,6 +540,7 @@ QgsException.__doc__ = "Defines a QGIS exception class." | |||
|
|||
try: | |||
import numpy as _numpy | |||
import shapely as _shapely |
There was a problem hiding this comment.
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
What about geometries with Z/M coordinates? |
By utilizing QgsGeometry.asWkb() this is ~8.45 times faster than the previous implementation.
python/PyQt6/core/__init__.py.in
Outdated
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) |
There was a problem hiding this comment.
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:
shapely_geom = _shapely.from_wkb(wkb_bytes) | |
shapely_geom = _shapely.from_wkb(wkb_qbytearray.data()) |
The QGIS project highly values your contribution and would love to see this work merged! Unfortunately this PR has not had any activity in the last 14 days and is being automatically marked as "stale". If you think this pull request should be merged, please check
|
This pull request has been tagged for the changelog.
You can edit the description. Format available for credits
Thank you! |
@merydian I am now getting a runtime error on QGIS start:
This is on ubuntu 22.04, shapely 1.8.0 I was able to fix this by doing: import shapely.geometry as _sg and then rewriting the offending line: def _geometry_as_shapely(self) -> _sg.base.BaseGeometry: Not sure why that does not work for me, and whether the fix is correct, but I assume others may get a similar issue... |
For the record, on ubuntu 24.04, shapely 2.0.3 things work fine without any modifications 🙂 |
Might this be a There seem to be other people having similar issues. I think your fix is simple enough to just include? Tagging @nyalldawson for some input please. |
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.