Using Custom Projections in Calculation Fields
Earlier this year we launched calculation fields, and we’re constantly impressed with the ways our customers are using them to add value to their field surveys. I want to demo a neat technique you can use to embed proj4js into a calculation field to gain access to custom GIS projections.
First, I’ll just show you the result:
The idea in the demo is pretty simple. The app uses the
LONGITUDE() functions to display and store the record location using different projections in separate calculation fields. In the demo app, the projection can also be changed on the fly using a choice field. Here is a table of the projections used in the demo app:
|EPSG Code||Proj.4 Text|
|EPSG:4326 WGS 84||
|EPSG:3086 NAD83 / Florida GDL Albers||
|EPSG:3857 Spherical Mercator||
|EPSG:3031 Antarctic Polar Stereographic (just for fun)||
|EPSG:26917 UTM zone 17N||
Here is the source of the “Proj4js” calculation field that exports a
proj4 function we can use in other calculation fields.
Once we have this calculation field setup, other calculation fields can use the library with
this.proj4. The technique of attaching code to
this in order to share it across calculation fields is important. Otherwise we would have to duplicate the entire proj4js library in each calculation field we wanted to use the library in.
Here is the code to use the main proj4js function to calculate the X value using a different projection:
It starts by pulling out the projection from the
projection choice field on the form. For demonstration purposes, the app lets you select the projection you want. The
proj4Text variable stores the proj4 text. You could also hardcode this text in the calculation for whatever projection you care about. If you aren’t familiar with proj4, that’s OK, you can use epsg.io or spatialreference.org to find the proj4 text for almost any projection.
To calculate the Y value, it uses a nearly identical expression except it sets the result to
result instead of
result. You can find more documentation on how to use proj4js on their website. If you just want to silently capture the projected X/Y values, you can hide all of the calculation fields and all of this will be invisible during the surveying.
Try it Yourself
Here is a link to the demo app in the app gallery. You can add it to your account to try it out yourself.