mbloch / mapshaper

Tools for editing Shapefile, GeoJSON, TopoJSON and CSV files
http://mapshaper.org
Other
3.76k stars 535 forks source link

feature request: set precision on export rather than import #44

Closed jasadams closed 10 years ago

jasadams commented 10 years ago

This would be useful for reducing the size of geojson files that are originally in high resolution but only a low resolution is required. The problem with reducing the precision on import is that it creates too many overlapping features particularly where there are lots of small features, which can create invalid geometry. Not sure what the issues are or how hard it is to implement, I just know that I would find this extremely useful

mbloch commented 10 years ago

This seems like a reasonable request. Before making the change I'd like to investigate the kinds of problems you're seeing when applying precision to datasets with lots of small features. I'm guessing that the same kinds of errors will also occur if rounding is applied on export (just fewer of them). It would be nice if mapshaper could repair the errors automatically.

The original reason for rounding coordinates on import was to provide a crude way of snapping together slightly misaligned polygon boundaries, so that mapshaper's topology function could identify shared boundaries correctly.

The --auto-snap and --snap-interval options were added after --precision. They provide a better way of snapping together misaligned coordinates.

jasadams commented 10 years ago

Here is what I was trying to do and what happened.

I am trying to reduce a shape file for viewing when zoomed right out. As it will be loaded in the browser I wanted the file to be as small as possible. When viewing the entire country of Australia, I did not think we needed 6 decimal places of resolution so I used the precision option to reduce this.

mapshaper -p 0.005 --precision 0.0001 -f geojson -o SA2_2011_AUST.json SA2_2011_AUST.shp

The "SA2_2011_AUST.shp" file is available from the ABS

No errors are reported, however when trying to create a 2dsphere index on the features in mongo, I get a "malformed geometry" for some of the features. On inspecting the features with these errors, I get invalid polygons as shown in the image below screen shot 2014-04-16 at 9 39 22

Zooming into the bottom left hand corner you see where the problem is. screen shot 2014-04-16 at 9 25 14

Running the above command but with 6 decimal places of precision results in no errors when loading into mongo and the following polygon for the same feature:

mapshaper -p 0.005 --precision 0.000001 -f geojson -o SA2_2011_AUST.json SA2_2011_AUST.shp

screen shot 2014-04-16 at 9 40 39

It would only be a small reduction in the file size if I was able to reduce the precision further so its not a big deal for me, the biggest cost for me was in the time trying to figure out what was going on. I'll leave it up to you to determine whether the effort involved in addressing this problem is worth it. Let me know if you need anything else from me

mbloch commented 10 years ago

This is very helpful, thanks.

Ideally, mapshaper should be able to round coordinates while avoiding the kind of geometrical errors that you ran into. Some of the cases that would need to be handled are:

It would be pretty easy to remove spikes, but handling self intersections is trickier.

A general solution for repairing corrupted polygons would be to increase the precision of coordinates in problem areas until the geometry is clean again.

(The same solution could be applied to geometrical errors in quantized TopoJSON output. This would require updating the relevant part of the TopoJSON spec to allow floating point coordinates in quantized datasets @mbostock)

mbloch commented 10 years ago

In the "better late than never" department, v0.2.4 has an option for setting coordinate precision on export. In addition to rounding coordinates, Mapshaper also tries to remove spikes and other geometry problems that might be introduced by rounding.

Mapshaper's syntax has changed a bit since this issue was first added. This is how the command would by written now:

mapshaper SA2_2011_AUST.shp -simplify 0.005 -o format=geojson precision=0.0001 SA2_2011_AUST.json
MarcusMapMaker commented 10 years ago

Hi Matthew,

Very useful research and a great implementation you've created with mapshaper. As the spatial data manager at the Australian Bureau of Stats I was just wondering why you selected Australian data to use in your example? No real reason just curious :-)

Dynamic generalisation is one of those ideas that I've yet to see implemented in online maps, but which will have a big impact when someone does it well.

cheers,

Marcus Blake Assistant Director Geography Section Australian Bureau of Statistics Marcus.Blake@abs.gov.au

Sent from my iPad

On 28 Aug 2014, at 5:33, Matthew Bloch notifications@github.com wrote:

In the "better late than never" department, v0.2.4 has an option for setting coordinate precision on export. In addition to rounding coordinates, Mapshaper also tries to remove spikes and other geometry problems that might be introduced by rounding.

Mapshaper's syntax has changed a bit since this issue was first added. This is how the command would by written now:

mapshaper SA2_2011_AUST.shp -simplify 0.005 -o format=geojson precision=0.0001 SA2_2011_AUST.json — Reply to this email directly or view it on GitHub.

jasadams commented 10 years ago

Hi Marcus,

I requested this feature as I am using mapshaper for a project I am working on and thought this feature would be useful to reduce the size of the geojson files. I will be trying it out in the coming weeks.

The project (https://territory.guru/) is Australian based and that is why I used an ABS shape file. I don't do dynamic generalisation but I do use mapshaper to create different resolutions based on zoom level.

Regards, Jason

mbloch commented 10 years ago

v0.2.4 added an option to set precision on output (-o precision=). v0.2.7 fixed a couple of related bugs. Closing the issue.