Merging and minifiying geo polygon data in elixir?

I am looking for an option to merge multiple geojson polygons to a single polygon, but could not find a library to do that with. I understand that merging minified shapes might lead to all kinds of artifacts (except if the polygons can be inflated a bit).

Checked out GitHub - felt/geo: A collection of GIS functions for Elixir, but could not find a function to merge shapes and GitHub - pkinney/topo: A Geometry library for Elixir that calculates spatial relationships between two geometries, which is more about finding relations between shapes.

Does a library to merge/minify polygons exists or is there a gap I am having in understanding the current libraries?

1 Like

I haven’t found one that does it, but if you are utilizing PostGIS, there are some functions there that can do it. ST_Union for example, ST_Union

There is the elixir library geo_postgis that wraps up quite a few of the functions into Ecto, Geo.PostGIS — GeoPostGIS v3.7.1

2 Likes

Good point!

In addition to this, if the GeoPostGIS library is missing a function from a newer postgis version, creating it on your own is very trivial, example from the library itself:

 defmacro st_transform(wkt, srid) do
    quote do: fragment("ST_Transform(?, ?)", unquote(wkt), unquote(srid))
  end
2 Likes

Thanks for the options!

Unfortunately this is just meant to be a “simple livebook”, so no database behind it.

Would probably then just export it as WKT from Elixir then, and fire up PostGIS and import the WKT and do the merging, export from PostGIS to WKT and to Elixir again

I’m with you. Most libraries around geo data seem to be about moving geo data less so than doing work on them. I recently implemented merging polygons (planar 2d, not geo data) manually in a project. For graphical stuff it would be great to have a library dealing with vector, line and polygon related math and operations – though I can see that that’s just part of the problem if you’re dealing with geo data and not just planar coordinates.

Unfortunately, a static shape is not helpful here, as the whole point of the application / livebook is for our sales managers to combine postal code areas (fixed georegions) as sales regions (dynamic).

They have an excel file with the lookup of postal codes to region. They can adjust the file to move/split/merge/create new regions and the livebook is the visual component for them to explore, verify and share the results.

I am going with coloring of regions for now, but would have loved to have a second “layer” on top, e.g. color by regional manager and border the independent subregions (or the other way round).

If there is no “quick” way of doing that for now, that is fine, would have been a nice bonus on top.

Ill accept the postgis answer as the most fitting one, as it might be the solution for slightly different use cases.

Thanks everyone!

Postgis is the best overall option, this is the better supported library out there for dealing with geo data.

If you want to simplify the process so that you can run livebooks without having a pg + postgis installed on each dev machine, you can deploy a dev instance and have everybody connect to it remotely, in this way you will have to setup it only once.

2 Likes

I’m not aware of a library to merge polygons together, but there is GitHub - pkinney/simplify_ex that will simplify a polygon using Ramer–Douglas–Peucker.

2 Likes

I haven’t looked at this stuff since 2019 but in the past when not having PostGIS to help I’ve shelled out to GDAL/OGR (ogr2ogr) command line utilities from Elixir apps to do a wide variety of vector and raster geo manipulation on files. It’s amazing what these OSS tools can do and I’d be shocked if they wouldn’t work for your use case.

I don’t have time to research now but I believe ogr2ogr may be able to do this for you:

Or you can use the tools to convert your GeoJSON to Spatialite or another format which may help also if you don’t want to bring PostGIS in. Enjoy the rabbit hole! :slight_smile:

2 Likes

I gave simplify_ex it a try and it worked well :slight_smile: Thanks for the hint!

The name did not strike me as “geodata related” so I missed it, even tho the repo is from the same creator as the topo library :sweat_smile:

The “merging polygons to have a single outline to highlight”-idea was replaced by simply giving each individual polygon in a group the same outline color instead of having their combined outline highlighted. So the fill can be used to display one information and the outline color to display another. This approach also does not clash with minifying the individual polygons beforehand, which now have simpler borders and therefore dont match up perfectly with their neighbours

1 Like