[R-sig-Geo] (no subject)
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Tue May 11 12:35:37 CEST 2010
On Tue, May 11, 2010 at 10:53 AM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
> On Mon, 10 May 2010, Stephen G. Eick wrote:
>
>> Are there any easy function to parse WKT and convert it into a sp object?
>> E.g. to parse
>>
>
> Not yet, not in OGR in rgdal anyway. There is a draft pair of MULTIPOLYGON
> <-> SpatialPolygons functions in rgeos on R-forge - maybe add a wish there,
> or consider contributing? GEOS has WKT input-output functions.
If you are desperate to parse some WKT then there's a couple of
solutions for python: the ogr and shapely libraries.
With ogr:
>>> f = ogr.CreateGeometryFromWkt("POINT(-88.06594 41.80628)")
then you can do:
>>> f.ExportToGML()
'<gml:Point><gml:coordinates>-88.065939999999998,41.80628</gml:coordinates></gml:Point>'
and you could collect up all the GML into a .gml file and read into R....
With shapely:
>>> from shapely import wkt
>>> s
'MULTILINESTRING ((-72.0000000000000000 45.0000000000000000,
-72.5000000000000000 47.7000000000000028, -73.0000000000000000
48.2000000000000028), (-82.0000000000000000 47.0000000000000000,
-82.5000000000000000 55.1000000000000014))'
>>> f = wkt.loads(s)
then get the coords thusly:
>>> [[p for p in g.coords] for g in f.geoms]
[[(-72.0, 45.0), (-72.5, 47.700000000000003), (-73.0,
48.200000000000003)], [(-82.0, 47.0), (-82.5, 55.100000000000001)]]
and do what you want with them.
The shapely package is looking very nice these days:
http://trac.gispython.org/lab/wiki/Examples
Barry
More information about the R-sig-Geo
mailing list