<div dir="ltr">HI Nyall,<div><br></div><div>Although the <b>projinfo command</b> works fine on a windows machine, I'm afraid I could not find <b>projinfo command</b> on my linux machine initially and even after installing PROJ (<span style="color:rgb(64,64,64);font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",Courier,monospace;font-size:12px">sudo apt-get install proj-bin</span>). 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 ).</div><div><br></div><div>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.</div><div><br></div><div>But, the 2nd suggestion would be worth trying. thanks for that.</div><div><br></div><div>Just to keep everyone on the same page, here is the way of creating QgsApplication I have followed.</div><div><pre style="background-color:rgb(43,43,43);color:rgb(169,183,198);font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(204,120,50)">from </span>qgis.core <span style="color:rgb(204,120,50)">import </span>QgsApplication<br><span style="color:rgb(204,120,50)">import </span>os<span style="color:rgb(204,120,50)">, </span>sys<br>os.environ[<span style="color:rgb(106,135,89)">"QT_QPA_PLATFORM"</span>] = <span style="color:rgb(106,135,89)">"offscreen"<br></span>QgsApplication.setPrefixPath(<span style="color:rgb(106,135,89)">r"/usr/share/qgis"</span><span style="color:rgb(204,120,50)">, True</span>)<br>sys.path.append(<span style="color:rgb(106,135,89)">'/usr/lib/qgis'</span>)<br>sys.path.append(<span style="color:rgb(106,135,89)">r'/usr/share/qgis/python/plugins'</span>)<br><span style="color:rgb(152,118,170);font-style:italic">qgs </span>= QgsApplication([]<span style="color:rgb(204,120,50)">, False</span>)<br><span style="color:rgb(152,118,170);font-style:italic">qgs</span>.initQgis()<br><span style="color:rgb(128,128,128)">### Entire business logic<br></span><span style="color:rgb(152,118,170);font-style:italic">qgs</span>.quit()</pre></div><div>-Prem</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, May 28, 2021 at 3:51 AM Nyall Dawson <<a href="mailto:nyall.dawson@gmail.com">nyall.dawson@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Thu, 27 May 2021 at 22:58, Prem Kumar <<a href="mailto:prem.netgis@gmail.com" target="_blank">prem.netgis@gmail.com</a>> wrote:<br>
><br>
> Hi Team,<br>
><br>
> Just wondering whether I'm missing something or a genuine bug, please advise below.<br>
><br>
> 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.<br>
><br>
> 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.<br>
<br>
Your transformation code looks fine, so the only issue could be that<br>
the proj library can't find the necessary data files. (Also the<br>
missing QgsApplication initialization which was pointed out earlier).<br>
Do the proj command line tools (such as "projinfo") work correctly for<br>
you?<br>
<br>
But while we're looking at this code, there's a lot of opportunity to<br>
optimise this!<br>
<br>
1. You don't need either the shapely or geojson modules to import<br>
geometries from geojson. Instead just directly convert GeoJSON<br>
features to QgsFeatures using QgsJsonUtils.stringToFeatureList() --<br>
the conversions to shapely objects and then to QgsGeometries is adding<br>
a LOT of overhead to your script.<br>
2. In this example script you actually don't need to use JSON at all,<br>
since you have a hardcoded list of coordinates. Instead you can make a<br>
QgsGeometry directly using something like:<br>
<br>
qgs_geom = QgsGeometry(QgsLineString([<br>
QgsPoint(-78.853385,43.858452), QgsPoint(-78.85593, 43.85792) ]) )<br>
<br>
that's the FASTEST way to create the QgsGeometry object, since there's<br>
no json/string parsing involved at all.<br>
<br>
Hope that helps!<br>
<br>
Nyall<br>
<br>
<br>
> Code Snippet for reproducing:<br>
><br>
> from qgis.PyQt.QtCore import QVariant<br>
> from shapely.geometry import shape<br>
> import geojson,json<br>
> from qgis.core import (QgsGeometry,QgsCoordinateReferenceSystem,QgsCoordinateTransform,QgsProject)<br>
> from shapely import speedups as sups<br>
> sups.disable()<br>
><br>
> in_geometry='[{"type": "LineString","coordinates": [[-78.85338577199997,43.85845267000008],[-78.85593885699996,43.857924291000074]]}]'<br>
> geomjson = json.loads(in_geometry)<br>
> old_crs = QgsCoordinateReferenceSystem("EPSG:4326")<br>
> new_crs = QgsCoordinateReferenceSystem("EPSG:3857")<br>
> xtransform = QgsCoordinateTransform(old_crs, new_crs, QgsProject.instance())<br>
> for i, g in enumerate(geomjson):<br>
>     s = json.dumps(g)<br>
>     g1 = geojson.loads(s)<br>
>     shapely_geom = shape(g1)<br>
>     qgs_geom=QgsGeometry.fromWkt(shapely_geom.wkt)<br>
>     qgs_geom.transform(xtransform)<br>
>     print (qgs_geom)<br>
><br>
> Output from Pycharm:<br>
> C:\Qgis\apps\Python37\python.exe C:/_WORK/SERVICE/test_transform.py<br>
> <QgsGeometry: LineString (-8777918.77684544585645199 5443563.52439526654779911, -8778202.97550544328987598 5443481.85537817236036062)><br>
><br>
> Process finished with exit code 0<br>
><br>
> Output from Linux terminal:<br>
> (gisenv) admin@rd-temp-server:~/gis_service$ python3 test_transform.py<br>
> Application path not initialized<br>
> Application path not initialized<br>
> <QgsGeometry: LineString (-78.85338600000000042 43.85845299999999725, -78.85593900000000644 43.85792399999999702)><br>
> (gisenv) admin@rd-temp-server:~/gis_service$<br>
><br>
> Thanks.<br>
> -Prem<br>
> _______________________________________________<br>
> QGIS-Developer mailing list<br>
> <a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
> List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
> Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div>