2016-09-08 1 views
0

저는 인도 Uttar Pradesh의 여러 지구 경계를 포함하는 TopoJSON 파일을 가지고 있습니다. 지도에 데이터를로드하면 지구 개요 만 표시됩니다. 지구 자체는 충만하지 않습니다.TopoJSON 선 스트링을 어떻게 다각형으로 변환합니까?

나는 각 지구가 일련 번호가 LineString로 구성된 geometries이있는 유형이 GeometryCollection 인 것이 문제라고 생각합니다.

대신 각 지구는 arcsPolygon 유형이어야합니다.

{ 
     "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]] 
    } 

내가 수동으로 고칠 수 있지만 보인다 미친 것 같은. 더 좋은 방법이 있습니까?

업데이트

그래서 나는 내가 원하는 생각 결과로 개체를 변환하는 방법을 알아 냈,하지만 난 아주 이상한 다각형을 얻었다. 여기에 내 (매우 clunky) 코드입니다. Saeed Adel Mehraban에게 감사의 말을 전합니다.
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의 참 Polygon의 변환 된 의미에서 "일". 그러나 다각형 자체는 모든 종류의 각도에서지도를 십자 모양으로 교차하는 직선을 가진 악몽입니다.

LineString s의 경계를 Polygon s로 변환 할 수있는 명령 줄 도구 같은 것이 있습니까?

+0

마찬가지로 GeoJSON 파일 형식으로 변환 할 수 있습니다. 그러나 나는 아직도 그것을 할 수있는 좋은 방법을 찾지 못했습니다. –

답변

0

아래와 같은 맵 기능이 있습니까?

var foo = [ 
 
    { 
 
    "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] 
 
     }] 
 
    }] 
 
    } 
 
]; 
 
var bar = foo.map(function(d) { 
 
    return { 
 
    "type": "Polygon", 
 
    "properties": d.geometries[0].properties, 
 
    "arc": d.geometries.map(function(g1) { 
 
     return g1.geometries.map(function(g) { 
 
     return g.arcs[0]; 
 
     }); 
 
    }) 
 
    }; 
 
}); 
 

 
console.log(bar);

(I 데이터 스키마에 대한 단순한 가정하에. 내가 topojson 형식에 익숙하지 않아요 이후 복잡한 선 스트링 작동을 보장 할 수없는 물품. 그러나 그것은 당신 제공하는 데이터와 함께 작동)
관련 문제