Select to view content in your preferred language

# Coordinates Conversion Based on Criteria Using Python

926
5
10-15-2020 03:45 PM
New Contributor III

I am trying to develop a custom tool to be used as a widget in Web AppBuilder. The tool should be able to return x, y coordinate values using default projection system (e.g. London Survey Grid) based on user’s click on the map. Then the returned x, y coordinate values should generate another set of x, y coordinate of the same location using some criteria (If and else statement).

The scenario is as below:

1. User clicks on the map and returns X, Y value (default projection system)

Let’s say X = 45670 and Y = 55000

2. Re-project or convert the returned X and Y values from step 1 to another set of coordinates of the same location (P2_X, P2_Y2)  using criteria below:

Criteria is: (This is just a sample, but I have a long real criteria)

If X == 0 and Y == 0:

return Projection2 (0,0)

elif X >= 44274 and X <= 45799 and Y >= 53927 and Y <= 54889:

P2_X = (X * 0.999942) – (Y * 0.2519)

P2_Y = (Y * 0.999942) + (X * 0.2519)

return Projection2(P2_X , P2_Y)

else:

P2_X = (X * 0.98888) – (Y * 0.4519)

P2_Y = (Y * 0.98888) + (X * 0.4519)

return Projection2(P2_X , P2_Y)

3. Then display both X, Y and P2_X, P2_Y

I have my python script that returns X,Y based on the user’s click on the map, but I am struggling in returning P2_X and P2_Y based on the criteria as mentioned above. I have a separate python script for the criteria. Please let me know if any one can help me on this, I can share my script if needed.

-Sushil

5 Replies
MVP Esteemed Contributor

if (X==0) and (Y==0):

...

elif (X >= 44274 and X <= 45799) and (Y >= 53927 and Y <= 54889):

Assuming your sample was supposed to be in python code, the above lines would have benefited from a proper equality check ( == ), the use of brackets to ensure or clarify proper expression parsing and colon was missing

If your sample was a generic expression, did you get error messages? or improper values? since I am not sure what the "code" represents

... sort of retired...
New Contributor III

Hi Thanks, for that. This was just the typo here but i do have == and : in my script properly coded. The main challenge here is how to relate and define X and Y coordinate value is equal to coordinate values returned from user's clicked point. That means the returned coordinate value should be equal to X, Y value in the script above. I am using  arcpy.da.SearchCursor(feature_record_set_layer, ['SHAPE@']) as cursor: for clicking and returning coordinate values.

Thanks,

-Sushil

MVP Esteemed Contributor

Still not sure how the script fits in, but SHAPE@ is going to return a list of 4 values, if it is a point (the last two values are for potential Z and M values.

You might want to use [SHAPE@X, SHAPE@Y], hence, conceptually

X, Y = [SHAPE@X, SHAPE@Y]

I can only assume you are already getting the point from the click (or are you?)

... sort of retired...
New Contributor III

Hi Dan,

Thanks. I can share my full script to you if i have your email if that's ok? I thought it might be easier in that way!

Thanks

MVP Esteemed Contributor

You can project to a different coordinate system within the search cursor

``````fc2 = r"C:\Git_Dan\npgeom\Project_npg\tests.gdb\sq" # ---- a sample featureclass

import arcpy
SR0 = arcpy.da.Describe(fc2)['spatialReference']
SR1 = arcpy.SpatialReference(4326)

# ---- in its native projected coordinate system
with arcpy.da.SearchCursor(fc2, ['SHAPE@X', 'SHAPE@Y'], spatial_reference=SR0, explode_to_points=True) as cur:
a = cur._as_narray()

# ---- projected to a GCS
with arcpy.da.SearchCursor(fc2, ['SHAPE@X', 'SHAPE@Y'], spatial_reference=SR1, explode_to_points=True) as cur:
b = cur._as_narray()

a
array([( 300009.00,  5000001.00), ( 300000.00,  5000000.00), ( 300002.00,  5000008.00), ( 300008.00,  5000010.00),
( 300010.00,  5000010.00), ( 300010.00,  5000008.00), ( 300009.00,  5000001.00), ( 300003.00,  5000003.00),
( 300007.00,  5000003.00), ( 300005.00,  5000007.00), ( 300003.00,  5000003.00), ( 300010.00,  5000008.00),
( 300010.00,  5000010.00), ( 300008.00,  5000010.00), ( 300008.00,  5000011.00), ( 300008.00,  5000012.00),
( 300012.00,  5000012.00), ( 300012.00,  5000008.00), ( 300010.00,  5000008.00), ( 300008.00,  5000011.00),
( 300005.00,  5000010.00), ( 300005.00,  5000012.00), ( 300006.00,  5000012.00), ( 300008.00,  5000012.00),
( 300008.00,  5000011.00), ( 300006.00,  5000012.00), ( 300005.00,  5000012.00), ( 300005.00,  5000015.00),
( 300007.00,  5000014.00), ( 300006.00,  5000012.00)], dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8')])

b
array([(-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14),
(-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14),
(-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14),
(-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14),
(-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14), (-76.56,  45.14)],
dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8')])
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

... sort of retired...