I have a simple (and maybe stupid) question for you today.

I have to compute intersection points between GIS polygons and imaginary lines I draw thanks to a script of mine... And I wonder if it would be relevant to use PostGIS instead of a Math module to catch intersection points.

Thank you.

EDIT: by math module (or library) I think about a mathematic module as I work on a Python script. So that is a "python math module".

  • 1
    PostGIS has a function called ST_INTERSECTIONS(geom1, geom2) which returns the interestions of two geometries. I use it with a WHERE ST_INTERSECTS(geom1, geom2). This sounds like it'll do what you're asking. I don't know what "imaginary" means in the context of geometries, though.
    – canisrufus
    Sep 1 '11 at 13:06
  • @canisrufus: "imaginary" means "not a data extracted from a real geographical information from our planet earth"... it means "a line I draw on a real map". Thank for the ST_INTERSECTIONS I know it but I wonder how fast it can be against a Math library...
    – lvictorino
    Sep 1 '11 at 13:40
  • What exactly do you mean by a "Math module" or "Math library"?
    – whuber
    Sep 1 '11 at 14:12
  • @whuber oh! sorry, let me edit my question for that "module/library" thing.
    – lvictorino
    Sep 1 '11 at 18:59
  • 1
    Thanks, but the update's not very specific. What functions does this library support? In particular, does it provide computational geometry functionality to manage and search polylines and polygons, or is it at a lower level than that?
    – whuber
    Sep 1 '11 at 19:03

Keep in mind that ST_INTERSECTIONS will make use of a spatial index whereas your math library may not. Not only must you determine if a line intersects a polygon, but which polygons to test for each line. A good spatial index with possibly fish-netting of these polygons can vastly speed up a spatial query like this. Not to mention if you have a large amount of polygons, you'll save time by not having to load all of the polygons into memory.

Unless you are loading a lot of data from this query from PostGIS over a slow network on another machine, the overhead is probably negligible.


If by "using a math module," you mean manually writing out formulas to compute an intersection, then you should really use PostGIS. No sense in reinventing the wheel!

Do note, however, that your data will need to be loaded into a PostGIS database.

So if your original data came in the form of shapefiles, you'll need to load that into a PostGIS database with shp2pgsql, for example. And if your script is generating a start- and endpoint for a line as human-readable (x,y) coordinates, you would need to convert those coordinates into a geometry using ST_GeomFromText.


I think that the Shapely Python package would work great for what you need. It uses the exact same geometry library that PostGIS uses, without the need for an underlying RDBMS.


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.