开发者

Google maps polygon optimization

I extracted country outline data from somewhere and successfully managed to conve开发者_运维知识库rt it into an array of lat-lng coordinates that I can feed to Google maps API to draw polyline or polygons.

The problem is that that there are about 1200+ points in that shape. It renders perfectly in Google maps but I need to reduce the number of points from 1200 to less than 100. I don't need a very smooth outline, i just need to throw away the points that I can live without. Any algorithm or an online tool that can help me reduce the number of points is needed.


Found this simple javascript by Bill Chadwick. Just feed in the LatLng to an array and pass in to the source arguments in a function here Douglas Peucker line simplification routine

it will output an array with less points for polygon.

 var ArrayforPolygontoUse= GDouglasPeucker(theArrayofLatLng,2000) 
 var polygon=new google.maps.Polygon({ 

    path:ArrayforPolygontoUse,
    geodesic:true,
    strokeColor:"#0000FF",
    strokeOpacity:0.8,
    strokeWeight:2,
    fillColor:"#0000FF",
    fillOpacity:0.4,
    editable:true
  });

theArrayofLatLng is an array of latlng that you collected using google maps api. The 2000 value is kink in metres. My assumption is, the higher the value, more points will be deleted as an output.

For real beginners: Make sure you declare the js file on your html page before using it. :)

<script type="text/javascript" src="js/GDouglasPeucker.js"></script>


I think MapShaper can do this online

Otherwise, implement some algorithm


If you can install postgis which i think is easy as they provide an installer then you can import the data and execute snaptogrid() or st_simplify() for which i cannot find an equivalent in mysql.If you decide to go with postgis which i recommend cause it will help you down the road i can provide you with the details.

Now for an easy custom solution you can reduce size by cutting or rounding some of the last digits of the coords and then merge the same coords resulting actually in a simple snaptogrid().

Hope it helps


I was looking for exactly the same thing and found Simplify.js. It does exactly what you want and is incredibly easy to use. You simply pass in your coordinates and it will remove all excess points.

Google maps polygon optimization

simplify(points, tolerance, highQuality)

The points argument should contain an array of your coordinates formatted as {x: 123, y: 123}. (Afterwards you can convert it back to the format you wish.)

The tolerance should be the precision in decimal degrees. E.g. 0.0001 for 11 meters. Increasing this number will reduce the output size.

Set highQuality to true for better results if you don't mind waiting a few milliseconds longer.


Mostly likely what you want to divide the points into 2 half and want to try my Javascript function:

function shortenAndShow ( polyline, color ) {
  var dist = 0, copyPoints = Array ( );
  for ( var n = 0, var end = polyline.getVertexCount ( ) - 1; n < end ; n++ ) {
    dist += polyline.getVertex ( n ).distanceFrom ( polyline.getVertex ( n +1 ) );
    copyPoints.push ( polyline.getVertex (n) );
   }
   var lastPoint = copyPoints [copyPoints.length-1];
   var newLine = new GPolyline (copyPoints, color, 2, 1);
   gmap2.addOverlay ( newLine );
} 


I agree the Unreason's anwser,The website support GeoJson,I used it in my website,and it cut down my geoJson ,But I think you also need this world country geo Json

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜