[geos-devel] how to construct geos geometry instances using python bindings

Sean Gillies sgillies at frii.com
Mon Mar 5 08:42:22 PST 2007

N.J. Hardebol wrote:
> Hello,
> I'm a medium level ocassional developper with not too much experience with
> python classes and especially not swig. I use python scripting a lot for
> medium level scripting for processing of geographic oriented data. Thereto
> I make use of gdal.ogr and geos libraries ported to python. Previously I
> ran my code under linux, allowing to build from newest source codes.
> Recently I moved code also to windows to interact also with ArcGIS, so
> limitted by available bin + swig compilations.
> I've two questions, both to do with the construction of new geos geometry
> instances.
> In first problem, I start with WKT or WKB geometry strings retrieved from
> postgis database. Afterwards I want to parse them to geos Geometry
> instances.
>>>> geosgeom = geos.WKTReader(geos.GeometryFactory()).read(wktgeom)
> This looks really awkward and as fact of the matter it doesn't build a
> geos.Geometry but a geos.GeometryPtr.
> So my Question1 is: How do i best build a geos Geometry from WKT-strings
> using geos-python bindings?
> Then I also would like to crop an existing geos Linestring1 with an
> intersecting geos Polygon building a new geos Linestring2. So something
> like  Linestring2 = Linestring1.crop(Polygon). My workaround is (1) check
> whether intersect exists. (2) Find the intersection points between
> Linestring1 and Polygon. (3) Build the new linestring2 from the
> intersection-point vertices using WKTReader().
> 1) >>> if Linestring1.intersects(Geometry):
> 2) >>>      pnts = Linestring1.Intersection(Geometry)
> 3) >>>      Linestr2_wkt = 'LINESTRING ( %s %s , %s %s ) % ( pnts.GetX(0)
>                          , pnts.GetY(0) , pnts.GetX(1) , pnts.GetY(1)  )
> 3) >>>      Linestring2 =
>                   geos.WKTReader(geos.GeometryFactory()).read(Linestr2_wkt)
> Question2: Is there a better way for building a new linestring by corpping
> old one with polygon, instead of this work around. ANd if not, how do I
> deal with GeometryPtr instead of Geometry, okay might be simple sub class
> or pointer issue this guy is not familiar with.
> thx in advance for any suggestions,
> Nico Hardebol


OGR geometries acquire the operations of GEOS geometries. I think you 
may not need to use the GEOS Python module at all. At any rate, the 
Python module in GEOS is not actively maintained.


Sean Gillies

More information about the geos-devel mailing list