Google maps polygon optimization

流过昼夜 提交于 2019-12-14 04:16:54

问题


I extracted country outline data from somewhere and successfully managed to convert 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.


回答1:


I think MapShaper can do this online

Otherwise, implement some algorithm




回答2:


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>



回答3:


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




回答4:


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 );
} 



回答5:


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




回答6:


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.

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.



来源:https://stackoverflow.com/questions/5298944/google-maps-polygon-optimization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!