Here's a JavaScript code snippet that uses the ArcGIS API for JavaScript to retrieve all vertices of a polyline feature layer from ArcGIS Online. This can be adapted to work in a web application or script, and the vertices can be used in Survey123 by exporting or processing them further:
// Include the ArcGIS API for JavaScript (ensure you have access to the API)
require([
"esri/layers/FeatureLayer",
"esri/Graphic",
"dojo/domReady!"
], function(FeatureLayer) {
// Replace with your hosted feature layer URL (use the ${layerurl} placeholder)
const layerUrl = "${layerurl}"; // Example: "https://your-org.maps.arcgis.com/arcgis/rest/services/YourLayer/FeatureServer/0"
const featureLayer = new FeatureLayer({
url: layerUrl
});
// Function to get all vertices from the polyline layer
async function getAllVertices() {
try {
// Create a query to get all features
const query = featureLayer.createQuery();
query.outFields = ["*"]; // Include all attributes
query.returnGeometry = true;
// Execute the query
const results = await featureLayer.queryFeatures(query);
// Collect all vertices
const allVertices = [];
results.features.forEach(feature => {
const geometry = feature.geometry;
if (geometry.type === "polyline") {
geometry.paths.forEach(path => {
path.forEach(vertex => {
allVertices.push(vertex); // Each vertex is [x, y]
});
});
}
});
// Output the vertices (customize as needed)
console.log("All vertices:", allVertices);
// Example: Convert to a format for Survey123 (e.g., CSV)
const csvContent = allVertices.map(v => `${v[0]},${v[1]}`).join("\n");
console.log("CSV format:", csvContent);
return allVertices;
} catch (error) {
console.error("Error retrieving vertices:", error);
}
}
// Run the function
getAllVertices();
});
Steps to Use:
- Include the ArcGIS API: Ensure the ArcGIS JavaScript API is loaded in your HTML (replace the
require path if using a different setup).
- Replace the Layer URL: Use the provided
${layerurl} placeholder with your actual hosted feature layer URL.
- Run the Script: Execute this in a browser console or a Node.js environment (with appropriate adjustments for Node.js).
- Process Results: The vertices are stored in
allVertices as an array of [x, y] coordinates. You can:
- Output to console
- Convert to CSV
- Store in a file
- Upload to Survey123 via a CSV or JSON file
Notes for Survey123 Integration:
-
Export Vertices to CSV/JSON:
- Save the vertices to a file (e.g., CSV) and import into Survey123 as a layer or attachment.
// Example CSV creation (manual download)
const downloadCSV = (content, filename) => {
const blob = new Blob([content], { type: "text/csv" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
};
const csvContent = allVertices.map(v => `${v[0]},${v[1]}`).join("\n");
downloadCSV(csvContent, "vertices.csv");
-
Use in Calculations:
- If vertices need to be part of a Survey123 form, pre-process them into a table and reference them via a relationship or lookup table.
-
Spatial Analysis:
- For advanced use (e.g., distance calculations), store vertices as attributes in a feature layer connected to your Survey123 project.
Error Handling Considerations:
- Layer Accessibility: Ensure the feature layer is publicly accessible or the user has appropriate credentials.
- Large Datasets: For very large datasets, use pagination with
queryResultOptions to handle query limits.
- Coordinate System: Vertices are returned in the layer's spatial reference (e.g., Web Mercator). Reproject if needed (e.g., to WGS84).
Let me know if you need further adjustments for a specific use case!