[QGIS-Developer] Reg: QgsCoordinateTransform not working on Linux

Prem Kumar prem.netgis at gmail.com
Fri May 28 01:15:12 PDT 2021


HI Nyall,

Although the *projinfo command* works fine on a windows machine, I'm afraid
I could not find *projinfo command* on my linux machine initially and even
after installing PROJ (sudo apt-get install proj-bin). If you can help me
on how to get PROJINFO command running on linux, that would be really great
(very little troubleshooting options on this issue over the internet ).

And thank you very much for your suggestions on handling the code
betterway. In fact I have used QgsJsonUtils.stringToFeatureList()
initially but took shapely approach because I hardly receive 3 or 4 line
segments but eventually need to generate line midpoints, convex hull
polygon around the input lines, getting a buffer around the convex hull
etc, which are basically geometry level operations and that I thought I
won't need to create input lines layer and operate on the features again.

But, the 2nd suggestion would be worth trying. thanks for that.

Just to keep everyone on the same page, here is the way of creating
QgsApplication I have followed.

from qgis.core import QgsApplication
import os, sys
os.environ["QT_QPA_PLATFORM"] = "offscreen"
QgsApplication.setPrefixPath(r"/usr/share/qgis", True)
sys.path.append('/usr/lib/qgis')
sys.path.append(r'/usr/share/qgis/python/plugins')
qgs = QgsApplication([], False)
qgs.initQgis()
### Entire business logic
qgs.quit()

-Prem


On Fri, May 28, 2021 at 3:51 AM Nyall Dawson <nyall.dawson at gmail.com> wrote:

> On Thu, 27 May 2021 at 22:58, Prem Kumar <prem.netgis at gmail.com> wrote:
> >
> > Hi Team,
> >
> > Just wondering whether I'm missing something or a genuine bug, please
> advise below.
> >
> > All I am trying is, to take the geometry object in json format whose CRS
> is in WGS84 (EPSG:4326) and transform the geometry to EPSG:3857 CRS and
> eventually use it in further processing because rest of the processing is
> in EPSG:3857 CRS.
> >
> > I have written below code and it works like a charm on Windows 10 but it
> doesn't transform the geometry on Linux. Please advise if there is anything
> wrong or missing to add.
>
> Your transformation code looks fine, so the only issue could be that
> the proj library can't find the necessary data files. (Also the
> missing QgsApplication initialization which was pointed out earlier).
> Do the proj command line tools (such as "projinfo") work correctly for
> you?
>
> But while we're looking at this code, there's a lot of opportunity to
> optimise this!
>
> 1. You don't need either the shapely or geojson modules to import
> geometries from geojson. Instead just directly convert GeoJSON
> features to QgsFeatures using QgsJsonUtils.stringToFeatureList() --
> the conversions to shapely objects and then to QgsGeometries is adding
> a LOT of overhead to your script.
> 2. In this example script you actually don't need to use JSON at all,
> since you have a hardcoded list of coordinates. Instead you can make a
> QgsGeometry directly using something like:
>
> qgs_geom = QgsGeometry(QgsLineString([
> QgsPoint(-78.853385,43.858452), QgsPoint(-78.85593, 43.85792) ]) )
>
> that's the FASTEST way to create the QgsGeometry object, since there's
> no json/string parsing involved at all.
>
> Hope that helps!
>
> Nyall
>
>
> > Code Snippet for reproducing:
> >
> > from qgis.PyQt.QtCore import QVariant
> > from shapely.geometry import shape
> > import geojson,json
> > from qgis.core import
> (QgsGeometry,QgsCoordinateReferenceSystem,QgsCoordinateTransform,QgsProject)
> > from shapely import speedups as sups
> > sups.disable()
> >
> > in_geometry='[{"type": "LineString","coordinates":
> [[-78.85338577199997,43.85845267000008],[-78.85593885699996,43.857924291000074]]}]'
> > geomjson = json.loads(in_geometry)
> > old_crs = QgsCoordinateReferenceSystem("EPSG:4326")
> > new_crs = QgsCoordinateReferenceSystem("EPSG:3857")
> > xtransform = QgsCoordinateTransform(old_crs, new_crs,
> QgsProject.instance())
> > for i, g in enumerate(geomjson):
> >     s = json.dumps(g)
> >     g1 = geojson.loads(s)
> >     shapely_geom = shape(g1)
> >     qgs_geom=QgsGeometry.fromWkt(shapely_geom.wkt)
> >     qgs_geom.transform(xtransform)
> >     print (qgs_geom)
> >
> > Output from Pycharm:
> > C:\Qgis\apps\Python37\python.exe C:/_WORK/SERVICE/test_transform.py
> > <QgsGeometry: LineString (-8777918.77684544585645199
> 5443563.52439526654779911, -8778202.97550544328987598
> 5443481.85537817236036062)>
> >
> > Process finished with exit code 0
> >
> > Output from Linux terminal:
> > (gisenv) admin at rd-temp-server:~/gis_service$ python3 test_transform.py
> > Application path not initialized
> > Application path not initialized
> > <QgsGeometry: LineString (-78.85338600000000042 43.85845299999999725,
> -78.85593900000000644 43.85792399999999702)>
> > (gisenv) admin at rd-temp-server:~/gis_service$
> >
> > Thanks.
> > -Prem
> > _______________________________________________
> > QGIS-Developer mailing list
> > QGIS-Developer at lists.osgeo.org
> > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210528/cdd964ec/attachment-0001.html>


More information about the QGIS-Developer mailing list