10

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.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Do you need to do this once?Also do you use any kind of database which might have spatial extensions on it? – Argiropoulos Stavros Mar 14 '11 at 13:13
  • @Argiropoulos Stavros: just once. no databases involved; its just a html/javascript page. If database would help then I could import the data into a MySQL 5.1 database and perform whatever operation you suggest. – Salman A Mar 14 '11 at 13:26
  • Search for "encoded polyline". It'll reduce your 1200+ points to a ~40-byte string – Travis Webb Mar 14 '11 at 17:16
  • Potrace works nicely, though I suspect its not what you want for map data. Nevertheless, Inkscape has a good implementation of postrace in its _simplify_ command. – bobbogo Mar 14 '11 at 19:41
  • I was looking for the same thing and [Simplify.js](http://stackoverflow.com/a/19432947/881551) works great. – Anthony Hatzopoulos Oct 17 '13 at 17:09

6 Answers6

8

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>
Quchie
  • 453
  • 8
  • 16
5

I think MapShaper can do this online

Otherwise, implement some algorithm

ndequeker
  • 7,932
  • 7
  • 61
  • 93
Unreason
  • 12,556
  • 2
  • 34
  • 50
  • It is considered polite to explain negative scores; I provided link to an online tool that might do what was asked. – Unreason Mar 14 '11 at 13:28
  • Can I extract the coordinate information from the "shp" files generated by map shaper? – Salman A Mar 14 '11 at 13:32
  • Sorry, didn't have time to do more than downvote at that moment. The explanation is that it seemed unhelpful to offer a tool (and a vague advice to implement "some algorithm") as a solution to what *seemed* to be a question about how to program a solution. Now that the OP clarified that he doesn't need to program it, but only do it once, I'll withdraw the downvote as soon as the system lets me. – Vojislav Stojkovic Mar 14 '11 at 14:05
  • @Salman A, AFAIK, yes to a point that you can for example use shp2pgsql to import them to PostGIS – Unreason Mar 14 '11 at 14:50
  • 1
    Implemented the "Douglas-Peucker line simplification algorithm" to reduce ~1200 points down to 70. – Salman A Mar 15 '11 at 06:05
  • the Douglas-Peucker algorithm is implemented in the simplify method here: http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/PolyUtil.html#simplify-java.util.List-double- – Analizer Mar 03 '18 at 16:43
1

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

Argiropoulos Stavros
  • 9,436
  • 11
  • 61
  • 79
1

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.

enter image description here

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.

Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97
0

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

Sun
  • 75
  • 7
0

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 );
} 
Micromega
  • 12,486
  • 7
  • 35
  • 72