[QGIS-Developer] Reg: QgsCoordinateTransform not working on Linux
Ujaval Gandhi
ujaval at spatialthoughts.com
Thu May 27 07:19:53 PDT 2021
OK. Test the code in QGIS Python Console. If that works, you have a
problem with initialization. If that also doesn't work, print the result of
the transform which will give you the OperationResult (Explanation at
https://qgis.org/api/classQgsGeometry.html#a8baf383b737d73cafeabe09f5b0d7ef7)
that may give insight into the issue.
Ujaval Gandhi
Spatial Thoughts
On Thu, May 27, 2021, 7:42 PM Prem Kumar <prem.netgis at gmail.com> wrote:
> Hi Ujaval,
>
> Thanks for your reply. I have made the suitable code changes as you have
> suggested but I still see the same behaviour.
>
> In fact, the given code snippet in the email is an extract from my overall
> code where proper initialisation and disposing of qgis application object
> is followed. The only difference between running the script on windows and
> linux is that, Im running the script on linux machine in a python virtual
> environment where as windows, running it directly from qgis python
> distributable. Some additional information and thanks for the troubleshoot
> option.
>
> *Windows:*
> OS: Microsoft Windows 10 Enterprise
> QGIS: 3.10.10-A Coruña
> Python: Python 3.7.0
>
> *Linux: *
> OS Kernel: Ubuntu 18.04.3 LTS
> QGIS: '3.16.3-Hannover'
> Python: Python 3.6.9
>
> -Prem
>
>
>
>
>
>
>
> On Thu, May 27, 2021 at 6:50 PM Ujaval Gandhi <ujaval at spatialthoughts.com>
> wrote:
>
>> You are not initializing QGIS in your script. Try adding this at the
>> beginning of your script
>>
>> from qgis.core import QgsApplication
>>
>> qgs = QgsApplication([], False)
>> qgs.initQgis()
>>
>> [image: Logo] <https://spatialthoughts.com/>
>> Ujaval Gandhi
>> Spatial Thoughts
>> mobile: +91-8095684687
>> email: ujaval at spatialthoughts.com
>> [image: LinkedIn icon] <https://www.linkedin.com/in/spatialthoughts/> [image:
>> Twitter icon] <https://twitter.com/spatialthoughts>
>>
>>
>>
>> On Thu, May 27, 2021 at 6:27 PM 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.
>>> *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/20210527/c85a4b06/attachment.html>
More information about the QGIS-Developer
mailing list