Hi,
I am looking to return the lat and long of a geoshape centroid from my survey form. Essentially I want to pass the centroid lat and long to what3words for reverse geocoding. Then the what3words result would populate a note on the survey. I have seen
https://developers.arcgis.com/rest/analysis/api-reference/find-centroids.htm
but am not really sure how to implement it. Would somebody be able to point me in the right direction, or able to share some sample Javascript to accomplish this please?
Many thanks
Hi,
I was wondering if anyone has found a possible workaround for that? I am trying to find a way to generate the centroid coordinates of a geoshape created in Survey123 field app. Any ideas including the pulldata("@javascript" custom query would be super useful.
Hello,
I was curious if anyone happened to figure out how to get the lat/long of the centroid from a geoshape that is created in Survey123? This is something that would be quite helpful.
Thanks
Hello, I figured something out with a javascript. If this solution is an answer somewhere else, let me know.
In Survey123, it's not directly possible to calculate the centroid of a polygon drawn using a GeoShape question. However, there is a way to get an approximate centroid by using a custom JavaScript approach. Here's the solution I implemented:
1. Using a Custom JavaScript File:
- A JavaScript file is used to extract the coordinates of the polygon (in GeoJSON format) and calculate the centroid by averaging the X (longitude) and Y (latitude) coordinates of the points that make up the polygon.
The centroid.js file contains two separate functions:
Here’s the JavaScript code used to calculate the centroid:
// Function to extract coordinates from a GeoJSON geometry
function extractCoordinates(geometry) {
if (geometry && geometry.rings && geometry.rings.length > 0) {
return geometry.rings[0].map(function(point) {
return [point[0], point[1]]; // Extracting only longitude (X) and latitude (Y)
});
}
return [];
}
// Function to calculate the Centroid X (longitude)
function getCentroidX(geometry) {
let coordinates = extractCoordinates(geometry);
let sumX = coordinates.reduce(function(sum, coord) { return sum + coord[0]; }, 0);
return sumX / coordinates.length; // Average of the X coordinates
}
// Function to calculate the Centroid Y (latitude)
function getCentroidY(geometry) {
let coordinates = extractCoordinates(geometry);
let sumY = coordinates.reduce(function(sum, coord) { return sum + coord[1]; }, 0);
return sumY / coordinates.length; // Average of the Y coordinates
}
2. Configuring the Form in Survey123:
- GeoShape: Allows the user to draw a polygon.
- poly_wkt: Stores the geometry as a WKT string (use string(${polygon}) to convert the GeoShape into a string).
- centroid_x and centroid_y: These fields calculate the X and Y coordinates of the centroid using pulldata() to call the JavaScript file. The pulldata() function is used as follows:
3. Creating a GeoPoint in a Repeat:
-To store the centroid geometry in a spatial layer, I used a GeoPoint inside a repeat.
-I used concat(${centroid_y}, ' ', ${centroid_x}) to combine the centroid coordinates and create a GeoPoint geometry in the form.
-This allows storing the centroid as a GeoPoint in the map layer with the calculated coordinates.
-Don't forget to add calculationMode=always in bind::esri:parameters
Here’s a quick look at the table structure in Excel: