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

Merged
merged 5 commits into from
Sep 30, 2024

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
Copy link

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

  • that all unit tests are passing

  • that all comments by reviewers have been addressed

  • that there is enough information for reviewers, in particular

    • link to any issues which this pull request fixes

    • add a description of workflows which this pull request fixes

    • add screenshots if applicable

  • that you have written unit tests where possible
    In case you should have any uncertainty, please leave a comment and we will be happy to help you proceed with this pull request.
    If there is no further activity on this pull request, it will be closed in a week.

@github-actions github-actions bot added the stale Uh oh! Seems this work is abandoned, and the PR is about to close. label Sep 28, 2024
@nyalldawson nyalldawson removed the stale Uh oh! Seems this work is abandoned, and the PR is about to close. label Sep 29, 2024
@nyalldawson nyalldawson merged commit a7621a2 into qgis:master Sep 30, 2024
32 checks passed
@nyalldawson nyalldawson added PyQGIS Related to the PyQGIS API Changelog Items that are queued to appear in the visual changelog - remove after harvesting labels Sep 30, 2024
@qgis-bot
Copy link
Collaborator

@merydian

This pull request has been tagged for the changelog.

  • The description will be harvested so please provide a "nearly-ready" text for the final changelog
  • If possible, add a nice illustration of the feature. Only the first one in the description will be harvested (GIF accepted as well)
  • If you can, it's better to give credits to your sponsor, see below for different formats.

You can edit the description.

Format available for credits
  • Funded by NAME
  • Funded by URL
  • Funded by NAME URL
  • Sponsored by NAME
  • Sponsored by URL
  • Sponsored by NAME URL

Thank you!

@wonder-sk
Copy link
Member

@merydian I am now getting a runtime error on QGIS start:

Couldn't load SIP module.  
Python support will be disabled.

Traceback (most recent call last):  
  File "", line 1, in  
  File "/home/martin/qgis/git-master/build/output/python/qgis/__init__.py", line 86, in  
    import qgis.gui  
  File "/home/martin/qgis/git-master/build/output/python/qgis/gui/__init__.py", line 26, in  
    from qgis.core import Qgis as _Qgis  
  File "/home/martin/qgis/git-master/build/output/python/qgis/core/__init__.py", line 22754, in  
    def _geometry_as_shapely(self) -> _shapely.geometry.base.BaseGeometry:  
AttributeError: module 'shapely' has no attribute 'geometry'

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...

@wonder-sk
Copy link
Member

For the record, on ubuntu 24.04, shapely 2.0.3 things work fine without any modifications 🙂

@merydian
Copy link
Contributor Author

merydian commented Oct 7, 2024

Might this be a shapely issue?

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.

@zacharlie zacharlie added ChangelogHarvested This PR description has been harvested in the Changelog already. and removed Changelog Items that are queued to appear in the visual changelog - remove after harvesting labels Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ChangelogHarvested This PR description has been harvested in the Changelog already. Freeze Exempt Feature Freeze exemption granted PyQGIS Related to the PyQGIS API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants