Ich habe eine TopoJSON-Datei mit den Grenzen verschiedener Bezirke in Uttar Pradesh, Indien. Wenn Sie die Daten auf einer Karte laden, sehen Sie nur die Umrisse der Bezirke. die Bezirke selbst sind nicht gefüllt.Wie konvertiert man TopoJSON Linestring in Polygon?
Ich glaube, das Problem ist, dass jeder Bezirk vom Typ GeometryCollection
ist, der seine eigene geometries
hat, die aus einer Reihe von LineString
s besteht.
Stattdessen möchte ich, dass jeder Bezirk vom Typ Polygon
ist, der gerade arcs
hat.
Zum Beispiel das erste Objekt:
{
"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]
}]
}
Ich glaube, ich es konvertieren wollen, und jedes andere Objekt, zu:
{
"type": "Polygon",
"properties": {
"district_number": 1,
"district_name": "Ghaziabad"
},
"arcs": [[0,1,2,3,4,5]]
}
Ich konnte es beheben manuell, aber das scheint wahnsinnig. Gibt es einen besseren Weg?
aktualisieren
So habe ich herausgefunden, wie das Objekt in das Ergebnis konvertieren Ich dachte, ich wollte, aber ich habe einige sehr verrückte Polygone. Hier ist mein (sehr klobiger) Code. Danke an Saeed Adel Mehraban für eine Anleitung.
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
});
});
Dieses „arbeitete“ in dem Sinne, dass meine LineString
s in der Tat zu Polygon
s umgewandelt wurden, die ursprüngliche Grenze zu halten. Aber die Polygone selbst sind ein Albtraum, mit geraden Linien, die die Karte in allen möglichen Winkeln durchziehen.
Gibt es so etwas wie ein Befehlszeilentool, das Grenzen von s in Polygon
s konvertieren kann?
Ebenso könnte ich dies in einem GeoJSON-Dateiformat konvertieren. Aber ich habe immer noch keinen guten Weg gefunden, es zu tun. –