所以我確實撰寫了一個簡單的函式來處理我在變數 poly 中宣告的字串,正如你所看到的,我使用了 split() 方法,現在我想將這些字串值中的每一個轉換為數值:
function toArray(polygon) {
final = polygon.replace('POLYGON ', '').replace('((', '').replace('))', '').split(',');
const arrOfNum = [];
final.forEach(str => {
arrOfNum.push(Number(str));
});
return arrOfNum
}
poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';
console.log(toArray(poly))
我正在嘗試將字串轉換為數值,但我得到了這個結果:
[NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]
稍后我想達到這個確切的點:
[[20.7218472,52.2294069],
[20.9436337,52.0756329],
[21.0651699,52.2134223],
[20.7767788,52.2537934],
[20.7218472,52.2294069]]
這些操作的主要目標是我想使用這些資料來確定一個點是否在一個多邊形內。為此,我正在使用此功能:
function ray_casting(point, polygon){
var n=polygon.length,
is_in=false,
x=point[0],
y=point[1],
x1,x2,y1,y2;
for(var i=0; i < n-1; i){
x1=polygon[i][0];
x2=polygon[i 1][0];
y1=polygon[i][1];
y2=polygon[i 1][1];
if(y < y1 != y < y2 && x < (x2-x1) * (y-y1) / (y2-y1) x1){
is_in=!is_in;
}
}
return is_in;
}
謝謝大家的幫助!
uj5u.com熱心網友回復:
你錯過了一些空間,需要處理對
正則運算式
function toArray(polygon) {
const final = polygon.match(/(\d \.\d \d \.\d )/g)
return final.flatMap(str => ([str.split(" ").map(str => str)]));
}
poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';
console.log(toArray(poly))
您的版本已擴展
function toArray(polygon) {
const final = polygon
.replace('POLYGON ((', '')
.replace('))', '')
.split(", ");
return final.flatMap(str => ([str.split(" ").map(str => str)]));
}
poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';
console.log(toArray(poly))
uj5u.com熱心網友回復:
此解決方案等效于@mplungjan 的解決方案之一。另一種方法是使用new Function或就像Function在第二個演示中一樣。
顯示代碼片段
const poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';
const nums = poly.replace(/POLYGON \(\(|\)\)/g, '')
.split(/, /).map(num => num.split(/ /).map(n => n));
console.log( nums );
使用new Function()
const poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))',
jstr = poly
.replace(/POLYGON \(\(/, '[[')
.replace(/\)\)/, ']]')
.replace(/, /g, '],[')
.replace(/ /g, ','),
output = new Function(`return ${jstr}`)();
console.log( output );
uj5u.com熱心網友回復:
您可以通過簡單地拆分陣列元素然后型別轉換為數字來簡單地實作它。
演示:
function toArray(polygon) {
final = polygon.replace('POLYGON ', '').replace('((', '').replace('))', '').split(',');
const arrOfNum = [];
final.forEach(str => {
const arr = [];
str.trim().split(' ').forEach(str => arr.push(Number(str)))
arrOfNum.push(arr);
});
return arrOfNum
}
poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';
console.log(toArray(poly))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/468114.html
標籤:javascript 数组 细绳 地理
