3
votes

I have a problem with the Google Maps API only allowing 1000 features per map.

In a database, I keep records of areas. Each area has associated with it a heap of KML polygon information, stored as an XML string. Each area is made up of one or more polygons.

Given some user input, a handful of these areas are combined into one <Placemark>. I get their combined geometry by wrapping the concatenated polygon information in a <MultiGeometry> tag.

<Placemark>
    <name>My combined area</name>
    <MultiGeometry>
        <Polygon> (area 1 info) </Polygon>
        <Polygon> (area 2 info) </Polygon>
        <Polygon> (area 2 info) </Polygon>
        <Polygon> (area 3 info) </Polygon>
    </MultiGeometry>
</Placemark>

The problem is that many of these areas are highly complex, and hence, any given Placemark could have over 100 polygons which very quickly pushes me of the limit of 1000 per document.

Now, given that the combined areas mostly form a single continuous area, there are a lot of lines and polygons on the inside of the continuous area which are quite useless. Is it possible to loop through the Polygons and merge them into one (or at least, fewer) polygons?

1
Well, yes, it's called a Small Matter of Programming. So to help we need to know more about the area info's that need to be combined, and when they need to stay apart. And when do you actually need all the detail? - Don
I can easily get a list of polygons that need to be merged into one (or more depending if the areas are continuous), and I don't ever need all the detail again after merging. - nickf

1 Answers

1
votes

The storage method makes this a very difficult problem to solve. Programmatically merging contiguous polygons is going to be slow and complicated.

Rather than storing XML fragments, push them into a GIS enabled database like PostgreSQL with PostGIS. This allows you to store the shape information as well known binary (WKB) objects rather than XML fragments, and gives you a full suite of GIS processing and formatting tools.

Once you have it that format this problem becomes very easy to solve. E.g. assuming the geometry column is called "the_geom", then you could use a query like the following:

SELECT ST_ASKML(ST_Union(the_geom)) AS area_union_askml
FROM areas 
WHERE (some_filter_expression)
GROUP BY (optional_group_by_expression)

This simply uses the aggregate function ST_UNION to combine the matched geometries in a single object and outputs the result column as a KML fragment.

If you need to simplify the shapes because the KML is too complicated for Google Maps, you can add a ST_Simplify or ST_SimplifyPreserveTopology. You could also use ST_NPoints to count the number of points in the resulting geometry so you can detect when you need to simplify the result.