我正在尝试编写一个脚本,该脚本将基于地理点经纬度按邻近度返回线段的标签。如果更容易的话,这也可能是调查开始后不久所拥有的设备。以下是 Esri 的 Ismael 完成的一些代码工作,我尝试对其进行修改。然而,我的java很糟糕。当我想提取字段标签的最近值时,下面的代码块会提取最近的记录。当我希望它指向存储在媒体文件夹 media/scat_segment.json 中的 json 时,它也指向一个 Web 服务。任何帮助将不胜感激
function getNearestRecord(featureLayer, location, searchWithinMeters,fields,where,token,debugmode){
if (featureLayer==="" || featureLayer===undefined){
featureLayer = "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Major_Cities/FeatureServer/0";
}
if (location==="" || location===undefined){
return (debugmode? "Location Object is empty":"");
}
if (fields==="" || fields===undefined){
fields="*";
}
if (where==="" || where===undefined){
where="1=1";
}
var coordsArray = location.split(" ");
var coords = coordsArray[1] + "," + coordsArray[0]
var xmlhttp = new XMLHttpRequest();
var url = featureLayer + "/query?geometry=" + coords + "&where=" + where + "&geometryType=esriGeometryPoint&inSR=4326&distance=" + searchWithinMeters + "&units=esriSRUnit_Meter&outFields=" + fields + "&returnGeometry=true&outSR=4326&f=json"
if (token){
url = url + "&token=" + token;
}
xmlhttp.open("GET",url,false);
xmlhttp.send();
if (xmlhttp.status!==200){
return (debugmode? xmlhttp.status:"");
} else {
var responseJSON=JSON.parse(xmlhttp.responseText)
if (responseJSON.error){
return (debugmode? JSON.stringify(responseJSON.error):"");
} else {
var count = responseJSON.features.length;
if (count===0){
return (debugmode? "No Features Found":"");
}
else if (count===1){
return JSON.stringify(responseJSON.features[0]);
}
else if (count>1){
var nearestIndex = findNearestIndex (location, responseJSON.features);
return JSON.stringify(responseJSON.features[nearestIndex]);
}
}
}
}
function findNearestIndex(location, features){
var nearestIndex = 0;
var minDistance = 999999999999;
var coordsArray = location.split(" ");
var lat1 = coordsArray[0];
var lon1 = coordsArray[1];
for (let i = 0; i < features.length; i++) {
var dist = getDistanceKM(lat1,lon1,features[i].geometry.y,features[i].geometry.x);
if (dist < minDistance){
minDistance = dist;
nearestIndex = i;
}
}
return nearestIndex;
}
function getDistanceKM(lat1, lon1, lat2, lon2) {
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
}
else {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
return dist * 1.609344;
}
}
我尝试自己编辑这些行,然后在调查 123 中返回了这个 我在 arcgis Survey 123 中的参考是:
我的代码块是
function getNearestLabel(filePath, segmentPoint, debugmode) {
if (!filePath || filePath === undefined) {
return (debugmode ? "File path is not provided" : "");
}
if (!segmentPoint || segmentPoint === undefined) {
return (debugmode ? "Segment point is not provided" : "");
}
var coordsArray = segmentPoint.split(" ");
var pointLat = parseFloat(coordsArray[0]);
var pointLon = parseFloat(coordsArray[1]);
// Assuming data is stored in a JSON file
var xmlhttp = new XMLHttpRequest();
var url = "media/scat_segment.json"; // Corrected file path
xmlhttp.open("GET", url, false);
xmlhttp.send();
if (xmlhttp.status !== 200) {
return (debugmode ? "Failed to fetch data from JSON file" : "");
} else {
var responseJSON = JSON.parse(xmlhttp.responseText);
var nearestIndex = findNearestLineIndex(pointLat, pointLon, responseJSON.features);
var nearestLabel = responseJSON.features[nearestIndex].attributes.Label;
return nearestLabel.toString(); // Convert to string before returning
}
}
function findNearestLineIndex(pointLat, pointLon, features) {
var nearestIndex = 0;
var minDistance = Number.MAX_VALUE;
for (var i = 0; i < features.length; i++) {
var coords = features[i].geometry.coordinates;
var dist = getDistanceToPoint(pointLat, pointLon, coords);
if (dist < minDistance) {
minDistance = dist;
nearestIndex = i;
}
}
return nearestIndex;
}
function getDistanceToPoint(pointLat, pointLon, lineCoords) {
var minDistance = Number.MAX_VALUE;
for (var i = 0; i < lineCoords.length - 1; i++) {
var dist = distanceToSegment(pointLat, pointLon, lineCoords[i][1], lineCoords[i][0], lineCoords[i + 1][1], lineCoords[i + 1][0]);
minDistance = Math.min(minDistance, dist);
}
return minDistance;
}
// Function to calculate the distance from a point to a line segment
function distanceToSegment(pointLat, pointLon, lat1, lon1, lat2, lon2) {
var x0 = pointLon;
var y0 = pointLat;
var x1 = lon1;
var y1 = lat1;
var x2 = lon2;
var y2 = lat2;
var dx = x2 - x1;
var dy = y2 - y1;
var along = ((x0 - x1) * dx + (y0 - y1) * dy) / (dx * dx + dy * dy);
var x, y;
if (along <= 0) {
x = x1;
y = y1;
} else if (along >= 1) {
x = x2;
y = y2;
} else {
x = x1 + along * dx;
y = y1 + along * dy;
}
return Math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0));
}
遗憾的是,您无法在 ArcGIS Survey123 Connect JavaScript 函数中访问本地文件[1]。
您可以尝试将
scat_segment.json
传输到调查的 media
文件夹中的 CSV 文件,其中其中一列是 JSON 字符串:
name,json
scat_segment,"{""features"":[{""type"":""Feature"",""geometry"":{""type"":""Point"",""coordinates"":[102.0,0.5]},""properties"":{""prop0"":""value0""}},...]}"
通过
pulldata('mycsvfile', 'json', 'name', 'scat_segment')
[2] 将其加载到问题中,将返回的 JSON 字符串作为参数传递给您的 JavaScript 函数以在其中进行处理。