[Gdal-dev] OGRGeometry::Distance

Frank Warmerdam warmerdam at pobox.com
Sun Sep 18 11:27:11 EDT 2005


On 9/18/05, Matthew Perry <perrygeo at gmail.com> wrote:
> Hi folks,
>  
>    I'm new to the list so I'll just introduce myself real quick. I've been
> doing GIS and cartography for 4 years at Humboldt State University as a
> Natural Resources major and, although it's not my background per se, I've
> been getting heavily into programming these past few months. Most of my web
> mapping stuff is PHP/Mapscript but I've begun really like python/OGR/GDAL
> for doing my data processing.
>  
>    Anyways, I've run into a bit of an issue with the OGRGeometry::Distance
> method. Check out the following script:
>  
>      import ogr
>      # Create some test geometries
>      wkt = 'POINT(1 1)'
>      g1 = ogr.CreateGeometryFromWkt(wkt)
>      wkt = 'POINT(2 1)'
>      g2 = ogr.CreateGeometryFromWkt(wkt)
>  
>      b = g1.Buffer(2)
>      # Works fine so we know geos is enabled
>   
>      d = g1.Distance(g2)
>      # fails ... "Attribute Error: Geometry instance has no attribute
> 'Distance'"
> 
>  According to the docs at
> http://gdal.maptools.org/ogr/classOGRGeometry.html this
> should work, right? Or have there been recent changes that break this
> method? Are there any updated docs anywhere? I'm using gdal 1.3.0 packaged
> with FWTools 0.9.9 on ubuntu linux.

Matt,

I'm afraid the Distance method didn't make it into the OGR Python bindings
for some reason. 

I have added it in CVS. 

It turns out all that was missing was the wrapper method so you can just add
the Distance method in FWTools0.9.9/pymod/ogr.py near the bottom.  
Something like:

    def SymmetricDifference( self, other ):
        geom = _gdal.OGR_G_SymmetricDifference( self._o, other._o )
        if geom is not None and geom != 'NULL':
            return Geometry( obj = geom, thisown = 1 )
        else:
            return None
        
    def Distance( self, other ):
        return _gdal.OGR_G_Distance( self._o, other._o )

def BuildPolygonFromEdges( edges, bBestEffort=0, bAutoClose=0, Tolerance=0 ):
    _o = _gdal.OGRBuildPolygonFromEdges( edges._o, bBestEffort, bAutoClose,
                                         Tolerance )
    if _o is not None and _o != 'NULL':
        return Geometry( obj = _o, thisown = 1 )
    else:
        return None;

Best regards,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list