1

I have a huge shapefile (over 400MB) of an entire city that contains many polygons of the residential area of this city. I can check it on QGIS as the following:

enter image description here

This shapefile contains a huge number of polygons, and I'd like cut a small shapefile from it in a circular or squared area around a point. So, let's say I give a point to a system, I want to return all the polygons around this area inside an offset (just so I can send only this segmentation of the data to the frontend of my application).

I know I could convert this shapefile to GeoJSON and find what are the polygons that exist inside the area that I have created (using turf.js for example), but that would be a brute force solution and it wouldn't be efficient.

On the other hand, I know I could install PostgreSQL and PostGIS and implement solutions like this one.

My doubt is that if I can achieve the goal of finding all polygons inside a polygon efficiently using only the command line, Python or JavaScript without implementing a spatial index for that, working directly with the shapefile. Is it possible?

1 Answer 1

6

You can achieve this using ogr2ogr with

# SHP from Natural Earth Data
ogr2ogr out.shp ne_110m_admin_0_countries.shp -dialect SQLite -sql "SELECT * FROM ne_110m_admin_0_countries WHERE ST_intersects(ST_Buffer(ST_GeomFromText('POINT (1 43)', 4326), 10), ne_110m_admin_0_countries.geometry);"

To sort out execution with index, you may want to add after ogr2ogr the option --debug ON

PS: the buffer units are degrees here because using EPSG 4326 units. The points coordinates are longitude 1 and latitude 43. You may need to reproject if you want to use meters or directly use a layer that use a projection based on meters.

10
  • Using the time command... I've realized that the time it takes for doing this operation is the same with the index file .qix generated or not (6 seconds in my case). So, creating the spatial index is necessary in this case? Is it used automatically?
    – raylight
    Commented Apr 29, 2021 at 16:07
  • It seems here the index is not used as when using SQLite dialect, can't use the shp index according to gis.stackexchange.com/questions/153472/…
    – ThomasG77
    Commented Apr 29, 2021 at 16:48
  • Made an edit. Not sure it's faster (small dataset on my test so not sure if major speed difference)
    – ThomasG77
    Commented Apr 29, 2021 at 17:39
  • If you want to try directly with my dataset it's this one. I've been testing these two lines here. The second one is actually slower, it takes around 20 seconds while the first takes 6 seconds on my computer. However, when I use the debug mode I see that the index is being created as you said. I think the problem is that it's creating the index on the run instead of using the .qix that we've created.
    – raylight
    Commented Apr 29, 2021 at 22:31
  • So, in theory, if there's a way of creating the index before executing the second command and using it instead of creating it on the run... it could make it faster.
    – raylight
    Commented Apr 29, 2021 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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