У меня есть файл TopoJSON, содержащий границы различных районов в штате Уттар-Прадеш, Индия. Когда вы загружаете данные на карте, вы видите только очертания районов; сами районы не заполнены.Как вы преобразовываете Linearring TopoJSON в полигон?
Я считаю, что проблема заключается в том, что каждый район имеет тип GeometryCollection
, у которого есть свой geometries
, состоящий из серии LineString
s.
Вместо этого я хочу, чтобы каждый район имел тип Polygon
, который имеет только arcs
.
Например, первый объект:
{
"type": "GeometryCollection",
"geometries": [{
"type": "GeometryCollection",
"properties": {
"district_number": 1,
"district_name": "Ghaziabad"
},
"geometries": [{
"type": "LineString",
"arcs": [0]
}, {
"type": "LineString",
"arcs": [1]
}, {
"type": "LineString",
"arcs": [2]
}, {
"type": "LineString",
"arcs": [3]
}, {
"type": "LineString",
"arcs": [4]
}, {
"type": "LineString",
"arcs": [5]
}]
}
Я думаю, что я хочу, чтобы преобразовать его, и любой другой объект, чтобы:
{
"type": "Polygon",
"properties": {
"district_number": 1,
"district_name": "Ghaziabad"
},
"arcs": [[0,1,2,3,4,5]]
}
я мог бы исправить это вручную, но это кажется невменяемый. Есть ли способ лучше?
Update
Так что я понял, как преобразовать объект в результате я думал, что я хотел, но я получил некоторые очень дурацкие многоугольники. Вот мой (очень неуклюжий) код. Спасибо Саиду Аделю Мехрабану за некоторые рекомендации по этому поводу.
d3.json('map.topojson',function(error,data){ // get my json that needs to be converted
var arr = data.objects.collection.geometries; // this is the relevant array
var newArr = []; // in order to map each object, i need to put each one into a new array as a single-item array
arr.forEach(function(d,i){
var curr = [d];
newArr.push(curr);
})
newArr.forEach(function(e,i){ // now that i have my new array, i want to convert each object that contains a LineString into a Polygon
var output = e.map(function(d){
var arcsArr = []; // an empty array to push each single value of the LineString arcs into
return {
"type": "Polygon", // change the type to polygon
"properties": d.properties, // keep the properties
"arcs": d.geometries.map(function(g) { // a single key-value pair for arcs, made up of the individual arcs from the LineString
arcsArr.push(g.arcs[0]);
return [arcsArr]; // the array of arcs must be in another array
})
};
});
var output = output[0]; // get only the first item in the output array, which is the object i have modified
output.arcs = output.arcs[0]; // and change the arcs so we're only taking the first array (as we've duplicated the arrays)
$('body').append(JSON.stringify(output)+','); // append the whole thing to the body so I can copy it and paste it into the appropriate part of the JSON
});
});
Этот «работал» в том смысле, что мои LineString
s действительно были преобразованы в Polygon
с, сохраняя первоначальную границу. Но сами полигоны - это кошмар, с прямыми линиями, пересекающими карту на всех углах.
Есть ли что-то вроде инструмента командной строки, который может преобразовывать границы из LineString
s в Polygon
s?
Аналогичным образом, я мог бы преобразовать это в формате файла GeoJSON. Но я все еще не нашел хорошего способа сделать это. –