Select to view content in your preferred language

Creating a formatted JSON feed

776
3
Jump to solution
03-06-2019 06:08 AM
by Anonymous User
Not applicable

Hello, I am trying to create a JSON feed from a layer stored in AGO however the coordinates come in as Web Mercator and I need them as WGS84. Using the project function works but is excruciatingly slow

Is there any other way to achieve this?

import json
import time
from arcgis.gis import GIS
from arcgis.geometry import project
gis = GIS(
 profile = "AGOL")#credentials stored in windows identity manager
fc = gis.content.get('LayerID')#layer ID
flayer = fc.layers[0]
fset = flayer.query()

for f in fset.features:
    if f.attributes['GPS_Time'] != None:
        geom=f.geometry['paths'][0] 
        geomarray = []
        for coord in geom:
            geomarray.append({'x':coord[0],'y':coord[1]})
            projectgeom = project(geometries=geomarray, in_sr=3857, out_sr=4326) 
            data={"UploadRequest": {"Projection": 1, "DataLayer": 4, "Issues": [{"Locations": [{"Geometry": projectgeom,"Description": f.attributes['Condition']}],
 "Type": 1,
 "Priority": 3,
 "SourceId": "API-TEST:1",
 "StartTime": f.attributes['GPS_Time'],
 "Url": "https://www.google.com",
 "Description": "This is a test"
 }
 ]
 }
}
0 Kudos
1 Solution

Accepted Solutions
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Hi Joel,

You don't have to explicitly project all the coordinates. the query function can do it for you when you specify out_sr parameter.

Are you trying to get a JSON representation of the layer? I am asking because you didn't specify the out_fields, the default value is "*" which returns all fields. if this is what you want, it's straightforward to get the JSON representation:

fset = flayer.query(out_sr=4326)
josn_fset = fset.tojson‍‍

If you only want to cherry-pick some information from features in the layer, then get the dict representation and manipulate the dict before converting it to the json format

import json
dict_fset = fset.to_dict()
# do something on the dict_fset
josn.dumps(dict_fset)

Also, you can format the code in your post and make it easy to read, that will increase the change for you to get a correct answer. As we all know, indentation in Python is like { and } in some other languages like Javascript, C/C#, Java etc, formatting (white spaces) is actually essential.

In you code, it seems you performing a projection for every coordinates (you can have tens of thousands of coordinates if polygon or polylines are involved), that's why it's excruciatingly slow. 

View solution in original post

3 Replies
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Hi Joel,

You don't have to explicitly project all the coordinates. the query function can do it for you when you specify out_sr parameter.

Are you trying to get a JSON representation of the layer? I am asking because you didn't specify the out_fields, the default value is "*" which returns all fields. if this is what you want, it's straightforward to get the JSON representation:

fset = flayer.query(out_sr=4326)
josn_fset = fset.tojson‍‍

If you only want to cherry-pick some information from features in the layer, then get the dict representation and manipulate the dict before converting it to the json format

import json
dict_fset = fset.to_dict()
# do something on the dict_fset
josn.dumps(dict_fset)

Also, you can format the code in your post and make it easy to read, that will increase the change for you to get a correct answer. As we all know, indentation in Python is like { and } in some other languages like Javascript, C/C#, Java etc, formatting (white spaces) is actually essential.

In you code, it seems you performing a projection for every coordinates (you can have tens of thousands of coordinates if polygon or polylines are involved), that's why it's excruciatingly slow. 

by Anonymous User
Not applicable

Thank you, that is exactly the answer I was looking for. I knew re-projecting each feature was a slow was of doing it but I didn't see another way. I'm just getting started with the API so it's great to know I can bring it in differently.

Thanks again.

Joel

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

No worries Joel, I am glad it helped.

could you please promote the answer by marking it as the correct answer so that it might also help others with the same question.Cheers.

0 Kudos