On Thu, 27 May 2021 at 22:58, Prem Kumar <prem.net...@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@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@rd-temp-server:~/gis_service$ > > Thanks. > -Prem > _______________________________________________ > QGIS-Developer mailing list > QGIS-Developer@lists.osgeo.org > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer _______________________________________________ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer