A data collector takes a point using the location widget, and it is necessary to insert into the form what statistical area, county, zone, etc the point is within. This could be done in post-processing, but suppose this information is needed for conditional logic within the form – for example, the form may change to collect different data depending on where the point was taken.
One approach to solve this I considered was to get the polygon the point was within by sending the lat and lon of the point to my agency’s map server – but this would not work if the form were offline.
A better solution was to export the polygons I was interested in as GeoJSON data, including only the fields for the feature shapes and the data I was interested in obtaining. For polygons with complex edges like rivers or shorelines, I simplified the geometry to reduce the vertex count, since there is a 65k character limit on page-level JS.
The GeoJSON data was stored as variable in PageLevel JS, along with a function from GitHub here to determine if an point is within a polygon. The point is input as an x,y array and the polygon is represented as an array of x,y arrays.
Then a function was added to take an input lat/lon and iterate through each feature in the GeoJSON, along with each part of the feature for multi-feature parts, and test if the point is within the feature. If the point is within the feature, the desired attribute/attributes of the feature are appended to an array, allowing the function to handle overlapping polygons in the input GeoJSON. The argument returns each of the polygon attributes the input point intersects, sorted alphabetically and omitting duplicates.
A full example of this is on GitHub – this idea seems to work well for simple geometries like municipal/state boundaries, keeping in mind more complex geometries will contain more vertices.
Another idea would be to use the Haversine distance equation to determine the closest point/line/polygon to a collected point, but I have not had a need to implement this yet. Ideally, it would be great if this information could be passed as arrays from other forms rather than stored within the form as a GeoJSON. Example: based on an input location, find the nearest port/town/sample site etc from a Smart Table Search. Basically a Smart Spatial Search.
Please sign in to leave a comment.