Swap coordinate pairs in a Polygon

5088
5
Jump to solution
02-03-2015 03:43 AM
MaartenTromp
New Contributor III

Hi,

I have an "Poll an External Website for JSON" Input connector. The Polygon information in the json is wrapped in a string and looks like this:

"\"type\":\"polygon\",\"centerLat\":\"53.2841903320603\",\"centerLng\":\"6.1497098207473755\",\"polygonVertices\":[{\"lat\":53.284613668968,\"lon\":6.1477410793304},{\"lat\":53.28426089025,\"lon\":6.1476981639862},{\"lat\":53.283766995153,\"lon\":6.1516785621643},{\"lat\":53.284318617876,\"lon\":6.1517214775085}]}"

With some string processing I am able to make a valid Geometry and send it to an Hosted FeatureService. The polygon looks like this:

"{""rings"":[[[53.284613668968,6.1477410793304],[53.28426089025,6.1476981639862],[53.283766995153,6.1516785621643],[53.284318617876,6.1517214775085]]],""spatialReference"":{""wkid"":4326}}}"

The location is east of Somalia instead of the Netherlands because latitude and longitude are in the wrong order.

Is there a way to swap the coordinate pairs in the polygon?

Kind Regards, Maarten Tromp

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MaartenTromp
New Contributor III

Hi,

It looks like the extra quotes and missing closing vertices is not an issue because the polygons appear in the Hosted FeatureService correctly.

In Notepad++ the correct result is created by using this RegEx to Search:

([0-9]+\.[0-9]+?),([0-9]+(\.[0-9]+)?)

And then this to replace:

\2,\1

But how to get this to work in the Processor "Field Calculator (Regular Expression)"?

Found it out myself by using the "Field Calculator" Processor and use this as expression:

replaceAll(polygonString2,'([0-9]+\.[0-9]+?),([0-9]+(\.[0-9]+)?)','$2,$1')

Regards, Maarten Tromp

View solution in original post

5 Replies
XanderBakker
Esri Esteemed Contributor

Hi Maarten Tromp‌,

Are you able to use RegEx in the string processing? Otherwise you may want to use the sdk and extend it:

Extending the GeoEvent Extension—Real-Time Data Feeds and Sensors | ArcGIS for Server

Kind regards, Xander

0 Kudos
MaartenTromp
New Contributor III

Hi Xander Bakker, good to hear from you!

My RegEx knowledge is limited, one of those handy things that doesn't stick very well:-(

There is a RegEx Processor in GeoEvent:

Processors—Real-time Data Feeds and Sensors | ArcGIS for Server

Any idea How to swap the two numbers in the PolygonString with RegEx?

Regards, Maarten

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Maarten Tromp‌, nice to hear from you too and good to see you're doing some interesting stuff!

RegEx isn't my string either, but I do note you have some additional quotes in your output string.

{

"rings" : [

  [ [-97.06326,32.759], [-97.06298,32.755], [-97.06153,32.749], [-97.06326,32.759] ]

],

"spatialReference" : {"wkid" : 4326}

}

... and it woud be better to close the polygon (append the first item if the list of coordinates to the end of the list)

I would expect it to be possible to extract all ocurrences of lat and lon and form the correct list of coordinates using regex.

Maybe this can help: RegExr: Learn, Build, & Test RegEx

So, what do I hear, you're not going to the DevSummit this year?

I will be strange...

Good luck, Xander

MaartenTromp
New Contributor III

Hi,

It looks like the extra quotes and missing closing vertices is not an issue because the polygons appear in the Hosted FeatureService correctly.

In Notepad++ the correct result is created by using this RegEx to Search:

([0-9]+\.[0-9]+?),([0-9]+(\.[0-9]+)?)

And then this to replace:

\2,\1

But how to get this to work in the Processor "Field Calculator (Regular Expression)"?

Found it out myself by using the "Field Calculator" Processor and use this as expression:

replaceAll(polygonString2,'([0-9]+\.[0-9]+?),([0-9]+(\.[0-9]+)?)','$2,$1')

Regards, Maarten Tromp

View solution in original post

XanderBakker
Esri Esteemed Contributor

If the Processor Field calculator can be compared with the field calculator available at desktop and Python is supported you could try this (sorry no regex):

def constructJSON(in_string):

    in_string = in_string.replace('\"','')

    lst = in_string.split('polygonVertices:')

    use = str(lst[1][:-1]).replace("lat","'lat'").replace("lon","'lon'")

    lst_use = eval(use)

    lst_crds = []

    for dct_crd in lst_use:

        lst_crds.append([dct_crd['lon'], dct_crd['lat']])

    lst_crds.append(lst_crds[0])

    dct = {"rings":[],"spatialReference":{"wkid":4326}}

    dct["rings"] = [lst_crds]

    # return "'{0}'".format(dct)

    return dct

Exucute with:

in_string = '"\"type\":\"polygon\",\"centerLat\":\"53.2841903320603\",\"centerLng\":\"6.1497098207473755\",\"polygonVertices\":[{\"lat\":53.284613668968,\"lon\":6.1477410793304},{\"lat\":53.28426089025,\"lon\":6.1476981639862},{\"lat\":53.283766995153,\"lon\":6.1516785621643},{\"lat\":53.284318617876,\"lon\":6.1517214775085}]}"'

print constructJSON(in_string)

returns:

{'rings': [[[6.1477410793304, 53.284613668968], [6.1476981639862, 53.28426089025], [6.1516785621643, 53.283766995153], [6.1517214775085, 53.284318617876], [6.1477410793304, 53.284613668968]]], 'spatialReference': {'wkid': 4326}}

Not sure if the dictionary serves you or that it has to be casted to string.