2

I'm still getting my head around GIS and am using QGIS 3.6.2.

I'd like to do a simple count of points within countries. The problem is that many of my points are located on coastlines or islands, so my current counts come up short because they do not fall within the defined polygons of the country world map. I guess this may be a resolution issue of the countries shape file?

Is there a way to add a buffer zone of a couple of kilometers to coastal polygon borders - but not to land borders? Or is there a world countries shape file that reflects accuracy of google maps or similar?

I've attached a screenshot of an example of two points in the Floria keys as seen by QGIS and by Google Maps.

Google maps locations of 2 points on country land

QGIS map that clearly shows 1 of the 2 points outside of country polygon

  • Comment: I found more detailed maps now via GADM. That seems to do the trick although of course now it takes a lot longer to do any computing - but it seems to have been a resolution matter. If anyone knows a simple way how to add buffers to maritime borders, please do still comment. Thanks – jaschmibu Jun 16 at 16:37
  • I was going to make a similar suggestion, that you get a higher-resolution country layer. Another option is the 10m resolution Admin 0 - Countries layer from Natural Earth Data. – csk Jun 16 at 16:58
  • 1
    Adding coastline-only buffers turns out to be surprisingly difficult. If you can get a coastline layer that's split by country, you could do a single-sided buffer and dissolve that with the country layer. – csk Jun 16 at 17:01
  • Thanks! NE 10M data is unfortunately not exact enough. It's worse than the map i took the screenshot from. The GADM is great yet really brings my machine to the limits when using it - or crashes QGIS. – jaschmibu Jun 17 at 5:08
  • I found a solution through a helpful comment in another post: Create a negative buffer of all country polygons (only has to be a very small distance, e.g. 0.0001 degrees). Then use the 'Snap geometries to layer' tool and set the reference layer to the buffered layer, the inputs to the point layer, and the mode to 'prefer closest point' using a tolerance to your needs (in my case 0.001 degrees). Afterwards delete the buffered layer again and do a count of the snapped points in the original polygons. – jaschmibu Jun 29 at 5:44
2

I come from SQL land, so not sure how applicable this to QGIS.

But in SQL I would think of two tables: one with original country polygons, another with country polygons buffered by whatever coastal buffer you want to use. Of course, the "buffered countries" would overlap on the land side, but that's simple to fix: you only count points within "buffered country" that don't belong to some other "original country".

In practice you don't need to create actual "buffered country" table, rather use ST_DWithin to include buffer. But it is a good mental model. For a single country the query would be something like

select * from points p
where 
    -- within `buffer_distance` of our country
    ST_DWithin(p.geom, 
               (select geom from countries where country_id = 123),
               buffer_distance)
    -- not within any other country
    and not exists (select 1 from countries o 
                    where o.country_id <> 123 and 
                    st_intersects(p.geom, o.geom))

Or you can actually create "buffered countries" table and preprocess buffered polygons by subtracting from each one all other original countries - effectively removing land-side buffer.

There is still an issue of points within multiple "buffered countries" - if it matters, it can be fixed by finding nearest original country, with more complicated SQL though.

  • Thanks for this helpful suggestion and solutions to overlapping buffers. I'll consider getting this replicated in QGIS if i fail to get the analysis with the GADM map to work. – jaschmibu Jun 17 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.