Discussion:
[Community] ANN fastkml:
Christian Ledermann
2012-06-27 07:19:18 UTC
Permalink
Hello,
I have a first working draft of a library that reads and writes kml
files on github:
https://github.com/cleder/fastkml

Introduction
fastkml is a library to read, write and manipulate kml files.
The aims are to keep it simple and fast (using lxml if available).
Fast refers to the time you spend to write and read KML files
as well as the time you spend to get aquainted to the library or to
create KML objects. It provides a subset of KML and is aimed at
documents that can be read from multiple clients such as openlayers
and google maps rather than to give you all functionality that KML
on google earth provides.

Geometries are handled as shapely objects. This is a restriction
that I can live with and you will seldom find KML files that implement
more complex geometries.

Limitations
Geometries are limited to the geometry and multigeometry types shapely
provides (Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon
and LinearRing). While KML allows for more abstract MultiGeometries
consisting of a combination of Points, LineStrings and LinearRings,
this is not supported in fastkml

Usage
You can find more examples in the included tests.py file, here is a
from fastkml import kml
from shapely.geometry import Point, LineString, Polygon
k = kml.KML()
ns = '{http://www.opengis.net/kml/2.2}'
d = kml.Document(ns, 'docid', 'doc name', 'doc description')
f = kml.Folder(ns, 'fid', 'f name', 'f description')
k.append(d)
d.append(f)
nf = kml.Folder(ns, 'nested-fid', 'nested f name', 'nested f description')
f.append(nf)
f2 = kml.Folder(ns, 'id2', 'name2', 'description2')
d.append(f2)
p = kml.Placemark(ns, 'id', 'name', 'description')
p.geometry = Polygon([(0, 0, 0), (1, 1, 0), (1, 0, 1)])
f2.append(p)
print k.to_string(prettyprint=True)
'<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
<ns0:Document id="docid">
<ns0:name>doc name</ns0:name>
<ns0:description>doc description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Folder id="fid">
<ns0:name>f name</ns0:name>
<ns0:description>f description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Folder id="nested-fid">
<ns0:name>nested f name</ns0:name>
<ns0:description>nested f description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
</ns0:Folder>
</ns0:Folder>
<ns0:Folder id="id2">
<ns0:name>name2</ns0:name>
<ns0:description>description2</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Placemark id="id">
<ns0:name>name</ns0:name>
<ns0:description>description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Polygon>
<ns0:outerBoundaryIs>
<ns0:LinearRing>
<ns0:coordinates>0.000000,0.000000,0.000000
1.000000,1.000000,0.000000
1.000000,0.000000,1.000000
0.000000,0.000000,0.000000
</ns0:coordinates>
</ns0:LinearRing>
</ns0:outerBoundaryIs>
</ns0:Polygon>
</ns0:Placemark>
</ns0:Folder>
</ns0:Document>
</ns0:kml>'
Read a KML file
from fastkml import kml
doc = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="http://www.opengis.net/kml/2.2">
... <Document>
... <name>Document.kml</name>
... <open>1</open>
... <Style id="exampleStyleDocument">
... <LabelStyle>
... <color>ff0000cc</color>
... </LabelStyle>
... </Style>
... <Placemark>
... <name>Document Feature 1</name>
... <styleUrl>#exampleStyleDocument</styleUrl>
... <Point>
... <coordinates>-122.371,37.816,0</coordinates>
... </Point>
... </Placemark>
... <Placemark>
... <name>Document Feature 2</name>
... <styleUrl>#exampleStyleDocument</styleUrl>
... <Point>
... <coordinates>-122.370,37.817,0</coordinates>
... </Point>
... </Placemark>
... </Document>
... </kml>"""
k = kml.KML()
k.from_string(doc)
len(k.features())
1
len(k.features()[0].features())
2
k.features()[0].features()[1]
<fastkml.kml.Placemark object at 0x876a16c>
k.features()[0].features()[1].description
k.features()[0].features()[1].name
'Document Feature 2'
k.features()[0].features()[1].name = "ANOTHER NAME"
print k.to_string(prettyprint=True)
<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
<ns0:Document>
<ns0:name>Document.kml</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>1</ns0:open>
<ns0:Style id="exampleStyleDocument">
<ns0:LabelStyle>
<ns0:color>ff0000cc</ns0:color>
<ns0:scale>1.0</ns0:scale>
</ns0:LabelStyle>
</ns0:Style>
<ns0:Placemark>
<ns0:name>Document Feature 1</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Point>
<ns0:coordinates>-122.371000,37.816000,0.000000</ns0:coordinates>
</ns0:Point>
</ns0:Placemark>
<ns0:Placemark>
<ns0:name>ANOTHER NAME</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Point>
<ns0:coordinates>-122.370000,37.817000,0.000000</ns0:coordinates>
</ns0:Point>
</ns0:Placemark>
</ns0:Document>
</ns0:kml>
--
Best Regards,

Christian Ledermann

Nairobi - Kenya
Mobile : +254 702978914

<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don?t drive species to extinction

2) Don?t destroy a habitat that species rely on.

3) Don?t change the climate in ways that will result in the above.

}<(((*>
Sean Gillies
2012-06-27 16:25:58 UTC
Permalink
Hi Christian,

Have you looked at my Python geospatial data protocol doc?

https://gist.github.com/2217756

This would be a more general and less Shapely-dependent way to
represent geometries, if you like.

You might also announce on the python-gis-sig group:

https://groups.google.com/forum/?hl=en_US&fromgroups#!forum/python-gis-sig

On Wed, Jun 27, 2012 at 1:19 AM, Christian Ledermann
Post by Christian Ledermann
Hello,
I have a first working draft of a library that reads and writes kml
https://github.com/cleder/fastkml
Introduction
fastkml is a library to read, write and manipulate kml files.
The aims are to keep it simple and fast (using lxml if available).
Fast refers to the time you spend to write and read KML files
as well as the time you spend to get aquainted to the library or to
create KML objects. It provides a subset of KML and is aimed at
documents that can be read from multiple clients such as openlayers
and google maps rather than to give you all functionality that KML
on google earth provides.
Geometries are handled as shapely objects. This is a restriction
that I can live with and you will seldom find KML files that implement
?more complex geometries.
Limitations
Geometries are limited to the geometry and multigeometry types shapely
provides (Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon
and LinearRing). While KML allows for more abstract MultiGeometries
consisting of a combination of Points, LineStrings and LinearRings,
this is not supported in fastkml
Usage
You can find more examples in the included tests.py file, here is a
from fastkml import kml
from shapely.geometry import Point, LineString, Polygon
k = kml.KML()
ns = '{http://www.opengis.net/kml/2.2}'
d = kml.Document(ns, 'docid', 'doc name', 'doc description')
f = kml.Folder(ns, 'fid', 'f name', 'f description')
k.append(d)
d.append(f)
nf = kml.Folder(ns, 'nested-fid', 'nested f name', 'nested f description')
f.append(nf)
f2 = kml.Folder(ns, 'id2', 'name2', 'description2')
d.append(f2)
p = kml.Placemark(ns, 'id', 'name', 'description')
p.geometry = ?Polygon([(0, 0, 0), (1, 1, 0), (1, 0, 1)])
f2.append(p)
print k.to_string(prettyprint=True)
'<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
?<ns0:Document id="docid">
? ?<ns0:name>doc name</ns0:name>
? ?<ns0:description>doc description</ns0:description>
? ?<ns0:visibility>1</ns0:visibility>
? ?<ns0:open>0</ns0:open>
? ?<ns0:Folder id="fid">
? ? ?<ns0:name>f name</ns0:name>
? ? ?<ns0:description>f description</ns0:description>
? ? ?<ns0:visibility>1</ns0:visibility>
? ? ?<ns0:open>0</ns0:open>
? ? ?<ns0:Folder id="nested-fid">
? ? ? ?<ns0:name>nested f name</ns0:name>
? ? ? ?<ns0:description>nested f description</ns0:description>
? ? ? ?<ns0:visibility>1</ns0:visibility>
? ? ? ?<ns0:open>0</ns0:open>
? ? ?</ns0:Folder>
? ?</ns0:Folder>
? ?<ns0:Folder id="id2">
? ? ?<ns0:name>name2</ns0:name>
? ? ?<ns0:description>description2</ns0:description>
? ? ?<ns0:visibility>1</ns0:visibility>
? ? ?<ns0:open>0</ns0:open>
? ? ?<ns0:Placemark id="id">
? ? ? ?<ns0:name>name</ns0:name>
? ? ? ?<ns0:description>description</ns0:description>
? ? ? ?<ns0:visibility>1</ns0:visibility>
? ? ? ?<ns0:open>0</ns0:open>
? ? ? ?<ns0:Polygon>
? ? ? ? ?<ns0:outerBoundaryIs>
? ? ? ? ? ?<ns0:LinearRing>
? ? ? ? ? ? ?<ns0:coordinates>0.000000,0.000000,0.000000
? ? ? ? ? ? ?1.000000,1.000000,0.000000
? ? ? ? ? ? ?1.000000,0.000000,1.000000
? ? ? ? ? ? ?0.000000,0.000000,0.000000
? ? ? ? ? ? ?</ns0:coordinates>
? ? ? ? ? ?</ns0:LinearRing>
? ? ? ? </ns0:outerBoundaryIs>
? ? ? ?</ns0:Polygon>
? ? ?</ns0:Placemark>
? ?</ns0:Folder>
?</ns0:Document>
</ns0:kml>'
Read a KML file
from fastkml import kml
doc = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="http://www.opengis.net/kml/2.2">
... <Document>
... ? <name>Document.kml</name>
... ? <open>1</open>
... ? <Style id="exampleStyleDocument">
... ? ? <LabelStyle>
... ? ? ? <color>ff0000cc</color>
... ? ? </LabelStyle>
... ? </Style>
... ? <Placemark>
... ? ? <name>Document Feature 1</name>
... ? ? <styleUrl>#exampleStyleDocument</styleUrl>
... ? ? <Point>
... ? ? ? <coordinates>-122.371,37.816,0</coordinates>
... ? ? </Point>
... ? </Placemark>
... ? <Placemark>
... ? ? <name>Document Feature 2</name>
... ? ? <styleUrl>#exampleStyleDocument</styleUrl>
... ? ? <Point>
... ? ? ? <coordinates>-122.370,37.817,0</coordinates>
... ? ? </Point>
... ? </Placemark>
... </Document>
... </kml>"""
k = kml.KML()
k.from_string(doc)
len(k.features())
1
len(k.features()[0].features())
2
k.features()[0].features()[1]
<fastkml.kml.Placemark object at 0x876a16c>
k.features()[0].features()[1].description
k.features()[0].features()[1].name
'Document Feature 2'
k.features()[0].features()[1].name = "ANOTHER NAME"
print k.to_string(prettyprint=True)
<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
?<ns0:Document>
? ?<ns0:name>Document.kml</ns0:name>
? ?<ns0:visibility>1</ns0:visibility>
? ?<ns0:open>1</ns0:open>
? ?<ns0:Style id="exampleStyleDocument">
? ? ?<ns0:LabelStyle>
? ? ? ?<ns0:color>ff0000cc</ns0:color>
? ? ? ?<ns0:scale>1.0</ns0:scale>
? ? ?</ns0:LabelStyle>
? ?</ns0:Style>
? ?<ns0:Placemark>
? ? ?<ns0:name>Document Feature 1</ns0:name>
? ? ?<ns0:visibility>1</ns0:visibility>
? ? ?<ns0:open>0</ns0:open>
? ? ?<ns0:Point>
? ? ? ?<ns0:coordinates>-122.371000,37.816000,0.000000</ns0:coordinates>
? ? ?</ns0:Point>
? ?</ns0:Placemark>
? ?<ns0:Placemark>
? ? ?<ns0:name>ANOTHER NAME</ns0:name>
? ? ?<ns0:visibility>1</ns0:visibility>
? ? ?<ns0:open>0</ns0:open>
? ? ?<ns0:Point>
? ? ? ?<ns0:coordinates>-122.370000,37.817000,0.000000</ns0:coordinates>
? ? ?</ns0:Point>
? ?</ns0:Placemark>
?</ns0:Document>
</ns0:kml>
--
Best Regards,
Christian Ledermann
Nairobi - Kenya
Mobile : +254 702978914
<*)))>{
If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.
1) Don?t drive species to extinction
2) Don?t destroy a habitat that species rely on.
3) Don?t change the climate in ways that will result in the above.
}<(((*>
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
Oleksandr Huziy
2012-06-27 16:37:24 UTC
Permalink
Hi Sean,

how is your protocol better than wkt?

It is easier to create a geometry in shapely and then save it to shape file
using osgeo.ogr, so sometimes I use wkt or wkb to convert geometries from
shapely to GDAL.

#create a polygon using shapely
p = Polygon(shell=points)
#print p.wkt
pGdal = ogr.CreateGeometryFromWkb(p.wkb)


BTW: great job with shapely, thank you

Cheers

--
Oleksandr Huziy

2012/6/27 Sean Gillies <sean.gillies at gmail.com>
Post by Sean Gillies
Hi Christian,
Have you looked at my Python geospatial data protocol doc?
https://gist.github.com/2217756
This would be a more general and less Shapely-dependent way to
represent geometries, if you like.
https://groups.google.com/forum/?hl=en_US&fromgroups#!forum/python-gis-sig
On Wed, Jun 27, 2012 at 1:19 AM, Christian Ledermann
Post by Christian Ledermann
Hello,
I have a first working draft of a library that reads and writes kml
https://github.com/cleder/fastkml
Introduction
fastkml is a library to read, write and manipulate kml files.
The aims are to keep it simple and fast (using lxml if available).
Fast refers to the time you spend to write and read KML files
as well as the time you spend to get aquainted to the library or to
create KML objects. It provides a subset of KML and is aimed at
documents that can be read from multiple clients such as openlayers
and google maps rather than to give you all functionality that KML
on google earth provides.
Geometries are handled as shapely objects. This is a restriction
that I can live with and you will seldom find KML files that implement
more complex geometries.
Limitations
Geometries are limited to the geometry and multigeometry types shapely
provides (Point, LineString, Polygon, MultiPoint, MultiLineString,
MultiPolygon
Post by Christian Ledermann
and LinearRing). While KML allows for more abstract MultiGeometries
consisting of a combination of Points, LineStrings and LinearRings,
this is not supported in fastkml
Usage
You can find more examples in the included tests.py file, here is a
from fastkml import kml
from shapely.geometry import Point, LineString, Polygon
k = kml.KML()
ns = '{http://www.opengis.net/kml/2.2}'
d = kml.Document(ns, 'docid', 'doc name', 'doc description')
f = kml.Folder(ns, 'fid', 'f name', 'f description')
k.append(d)
d.append(f)
nf = kml.Folder(ns, 'nested-fid', 'nested f name', 'nested f
description')
Post by Christian Ledermann
f.append(nf)
f2 = kml.Folder(ns, 'id2', 'name2', 'description2')
d.append(f2)
p = kml.Placemark(ns, 'id', 'name', 'description')
p.geometry = Polygon([(0, 0, 0), (1, 1, 0), (1, 0, 1)])
f2.append(p)
print k.to_string(prettyprint=True)
'<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
<ns0:Document id="docid">
<ns0:name>doc name</ns0:name>
<ns0:description>doc description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Folder id="fid">
<ns0:name>f name</ns0:name>
<ns0:description>f description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Folder id="nested-fid">
<ns0:name>nested f name</ns0:name>
<ns0:description>nested f description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
</ns0:Folder>
</ns0:Folder>
<ns0:Folder id="id2">
<ns0:name>name2</ns0:name>
<ns0:description>description2</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Placemark id="id">
<ns0:name>name</ns0:name>
<ns0:description>description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Polygon>
<ns0:outerBoundaryIs>
<ns0:LinearRing>
<ns0:coordinates>0.000000,0.000000,0.000000
1.000000,1.000000,0.000000
1.000000,0.000000,1.000000
0.000000,0.000000,0.000000
</ns0:coordinates>
</ns0:LinearRing>
</ns0:outerBoundaryIs>
</ns0:Polygon>
</ns0:Placemark>
</ns0:Folder>
</ns0:Document>
</ns0:kml>'
Read a KML file
from fastkml import kml
doc = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="http://www.opengis.net/kml/2.2">
... <Document>
... <name>Document.kml</name>
... <open>1</open>
... <Style id="exampleStyleDocument">
... <LabelStyle>
... <color>ff0000cc</color>
... </LabelStyle>
... </Style>
... <Placemark>
... <name>Document Feature 1</name>
... <styleUrl>#exampleStyleDocument</styleUrl>
... <Point>
... <coordinates>-122.371,37.816,0</coordinates>
... </Point>
... </Placemark>
... <Placemark>
... <name>Document Feature 2</name>
... <styleUrl>#exampleStyleDocument</styleUrl>
... <Point>
... <coordinates>-122.370,37.817,0</coordinates>
... </Point>
... </Placemark>
... </Document>
... </kml>"""
k = kml.KML()
k.from_string(doc)
len(k.features())
1
len(k.features()[0].features())
2
k.features()[0].features()[1]
<fastkml.kml.Placemark object at 0x876a16c>
k.features()[0].features()[1].description
k.features()[0].features()[1].name
'Document Feature 2'
k.features()[0].features()[1].name = "ANOTHER NAME"
print k.to_string(prettyprint=True)
<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
<ns0:Document>
<ns0:name>Document.kml</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>1</ns0:open>
<ns0:Style id="exampleStyleDocument">
<ns0:LabelStyle>
<ns0:color>ff0000cc</ns0:color>
<ns0:scale>1.0</ns0:scale>
</ns0:LabelStyle>
</ns0:Style>
<ns0:Placemark>
<ns0:name>Document Feature 1</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Point>
<ns0:coordinates>-122.371000,37.816000,0.000000</ns0:coordinates>
</ns0:Point>
</ns0:Placemark>
<ns0:Placemark>
<ns0:name>ANOTHER NAME</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Point>
<ns0:coordinates>-122.370000,37.817000,0.000000</ns0:coordinates>
</ns0:Point>
</ns0:Placemark>
</ns0:Document>
</ns0:kml>
--
Best Regards,
Christian Ledermann
Nairobi - Kenya
Mobile : +254 702978914
<*)))>{
If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.
1) Don?t drive species to extinction
2) Don?t destroy a habitat that species rely on.
3) Don?t change the climate in ways that will result in the above.
}<(((*>
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20120627/78f66f1b/attachment-0001.htm>
Sean Gillies
2012-06-27 17:12:41 UTC
Permalink
Hi Oleksandr,

It's not better, just different. WKT is for sharing data between
different programs, even over a network. I designed this protocol for
passing geospatial data within a single Python program (or distributed
Python programs if you want) where it's not necessary to serialize
between modules or functions.

I'm glad you enjoy Shapely :)
Post by Oleksandr Huziy
Hi Sean,
how is your protocol better than wkt?
It is easier to create a geometry in shapely and then save it to shape file
using osgeo.ogr, so sometimes I use wkt or wkb? to convert geometries from
shapely to GDAL.
??????? #create a polygon using shapely
??????? p = Polygon(shell=points)
??????? #print p.wkt
??????? pGdal = ogr.CreateGeometryFromWkb(p.wkb)
BTW: great job with shapely, thank you
Cheers
--
Oleksandr Huziy
2012/6/27 Sean Gillies <sean.gillies at gmail.com>
Post by Sean Gillies
Hi Christian,
Have you looked at my Python geospatial data protocol doc?
https://gist.github.com/2217756
This would be a more general and less Shapely-dependent way to
represent geometries, if you like.
?https://groups.google.com/forum/?hl=en_US&fromgroups#!forum/python-gis-sig
On Wed, Jun 27, 2012 at 1:19 AM, Christian Ledermann
Post by Christian Ledermann
Hello,
I have a first working draft of a library that reads and writes kml
https://github.com/cleder/fastkml
Introduction
fastkml is a library to read, write and manipulate kml files.
The aims are to keep it simple and fast (using lxml if available).
Fast refers to the time you spend to write and read KML files
as well as the time you spend to get aquainted to the library or to
create KML objects. It provides a subset of KML and is aimed at
documents that can be read from multiple clients such as openlayers
and google maps rather than to give you all functionality that KML
on google earth provides.
Geometries are handled as shapely objects. This is a restriction
that I can live with and you will seldom find KML files that implement
?more complex geometries.
Limitations
Geometries are limited to the geometry and multigeometry types shapely
provides (Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon
and LinearRing). While KML allows for more abstract MultiGeometries
consisting of a combination of Points, LineStrings and LinearRings,
this is not supported in fastkml
Usage
You can find more examples in the included tests.py file, here is a
from fastkml import kml
from shapely.geometry import Point, LineString, Polygon
k = kml.KML()
ns = '{http://www.opengis.net/kml/2.2}'
d = kml.Document(ns, 'docid', 'doc name', 'doc description')
f = kml.Folder(ns, 'fid', 'f name', 'f description')
k.append(d)
d.append(f)
nf = kml.Folder(ns, 'nested-fid', 'nested f name', 'nested f description')
f.append(nf)
f2 = kml.Folder(ns, 'id2', 'name2', 'description2')
d.append(f2)
p = kml.Placemark(ns, 'id', 'name', 'description')
p.geometry = ?Polygon([(0, 0, 0), (1, 1, 0), (1, 0, 1)])
f2.append(p)
print k.to_string(prettyprint=True)
'<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
?<ns0:Document id="docid">
? ?<ns0:name>doc name</ns0:name>
? ?<ns0:description>doc description</ns0:description>
? ?<ns0:visibility>1</ns0:visibility>
? ?<ns0:open>0</ns0:open>
? ?<ns0:Folder id="fid">
? ? ?<ns0:name>f name</ns0:name>
? ? ?<ns0:description>f description</ns0:description>
? ? ?<ns0:visibility>1</ns0:visibility>
? ? ?<ns0:open>0</ns0:open>
? ? ?<ns0:Folder id="nested-fid">
? ? ? ?<ns0:name>nested f name</ns0:name>
? ? ? ?<ns0:description>nested f description</ns0:description>
? ? ? ?<ns0:visibility>1</ns0:visibility>
? ? ? ?<ns0:open>0</ns0:open>
? ? ?</ns0:Folder>
? ?</ns0:Folder>
? ?<ns0:Folder id="id2">
? ? ?<ns0:name>name2</ns0:name>
? ? ?<ns0:description>description2</ns0:description>
? ? ?<ns0:visibility>1</ns0:visibility>
? ? ?<ns0:open>0</ns0:open>
? ? ?<ns0:Placemark id="id">
? ? ? ?<ns0:name>name</ns0:name>
? ? ? ?<ns0:description>description</ns0:description>
? ? ? ?<ns0:visibility>1</ns0:visibility>
? ? ? ?<ns0:open>0</ns0:open>
? ? ? ?<ns0:Polygon>
? ? ? ? ?<ns0:outerBoundaryIs>
? ? ? ? ? ?<ns0:LinearRing>
? ? ? ? ? ? ?<ns0:coordinates>0.000000,0.000000,0.000000
? ? ? ? ? ? ?1.000000,1.000000,0.000000
? ? ? ? ? ? ?1.000000,0.000000,1.000000
? ? ? ? ? ? ?0.000000,0.000000,0.000000
? ? ? ? ? ? ?</ns0:coordinates>
? ? ? ? ? ?</ns0:LinearRing>
? ? ? ? </ns0:outerBoundaryIs>
? ? ? ?</ns0:Polygon>
? ? ?</ns0:Placemark>
? ?</ns0:Folder>
?</ns0:Document>
</ns0:kml>'
Read a KML file
from fastkml import kml
doc = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="http://www.opengis.net/kml/2.2">
... <Document>
... ? <name>Document.kml</name>
... ? <open>1</open>
... ? <Style id="exampleStyleDocument">
... ? ? <LabelStyle>
... ? ? ? <color>ff0000cc</color>
... ? ? </LabelStyle>
... ? </Style>
... ? <Placemark>
... ? ? <name>Document Feature 1</name>
... ? ? <styleUrl>#exampleStyleDocument</styleUrl>
... ? ? <Point>
... ? ? ? <coordinates>-122.371,37.816,0</coordinates>
... ? ? </Point>
... ? </Placemark>
... ? <Placemark>
... ? ? <name>Document Feature 2</name>
... ? ? <styleUrl>#exampleStyleDocument</styleUrl>
... ? ? <Point>
... ? ? ? <coordinates>-122.370,37.817,0</coordinates>
... ? ? </Point>
... ? </Placemark>
... </Document>
... </kml>"""
k = kml.KML()
k.from_string(doc)
len(k.features())
1
len(k.features()[0].features())
2
k.features()[0].features()[1]
<fastkml.kml.Placemark object at 0x876a16c>
k.features()[0].features()[1].description
k.features()[0].features()[1].name
'Document Feature 2'
k.features()[0].features()[1].name = "ANOTHER NAME"
print k.to_string(prettyprint=True)
<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
?<ns0:Document>
? ?<ns0:name>Document.kml</ns0:name>
? ?<ns0:visibility>1</ns0:visibility>
? ?<ns0:open>1</ns0:open>
? ?<ns0:Style id="exampleStyleDocument">
? ? ?<ns0:LabelStyle>
? ? ? ?<ns0:color>ff0000cc</ns0:color>
? ? ? ?<ns0:scale>1.0</ns0:scale>
? ? ?</ns0:LabelStyle>
? ?</ns0:Style>
? ?<ns0:Placemark>
? ? ?<ns0:name>Document Feature 1</ns0:name>
? ? ?<ns0:visibility>1</ns0:visibility>
? ? ?<ns0:open>0</ns0:open>
? ? ?<ns0:Point>
? ? ? ?<ns0:coordinates>-122.371000,37.816000,0.000000</ns0:coordinates>
? ? ?</ns0:Point>
? ?</ns0:Placemark>
? ?<ns0:Placemark>
? ? ?<ns0:name>ANOTHER NAME</ns0:name>
? ? ?<ns0:visibility>1</ns0:visibility>
? ? ?<ns0:open>0</ns0:open>
? ? ?<ns0:Point>
? ? ? ?<ns0:coordinates>-122.370000,37.817000,0.000000</ns0:coordinates>
? ? ?</ns0:Point>
? ?</ns0:Placemark>
?</ns0:Document>
</ns0:kml>
--
Best Regards,
Christian Ledermann
Nairobi - Kenya
Mobile : +254 702978914
<*)))>{
If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.
1) Don?t drive species to extinction
2) Don?t destroy a habitat that species rely on.
3) Don?t change the climate in ways that will result in the above.
}<(((*>
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
Stefano Costa
2012-06-27 16:46:06 UTC
Permalink
Post by Sean Gillies
This would be a more general and less Shapely-dependent way to
represent geometries, if you like.
Especially considered that the library is otherwise pure Python, I
would second Sean's suggestion. I wonder if the "geojson" package fits
the purpose or it's rather better to implement to protocol from scratch.

I have nothing against Shapely of course, but its dependency on GEOS
makes it more complicated to install with modern Python packaging
tools like pip.

If I'm not asking too much, could you explain how different fastkml is
from simplekml? http://pypi.python.org/pypi/simplekml/0.9.3

Thanks, and congratulations on this first release
steko
Christian Ledermann
2012-06-28 05:27:52 UTC
Permalink
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Post by Sean Gillies
This would be a more general and less Shapely-dependent way to
represent geometries, if you like.
Especially considered that the library is otherwise pure Python, I
would second Sean's suggestion. I wonder if the "geojson" package fits
the purpose
+ 1
or it's rather better to implement to protocol from scratch.
-1
I have nothing against Shapely of course, but its dependency on GEOS
makes it more complicated to install with modern Python packaging
tools like pip.
Good point I will look at geojson (that seems to fit my purposes). I
used shapely
mainly because it is tried, tested, well documented and I am
comfortable with it.
If I'm not asking too much, could you explain how different fastkml is
from simplekml? http://pypi.python.org/pypi/simplekml/0.9.3
Mainly it is the ability to load kml files, or construct features from their xml
Post by Sean Gillies
pm = Placemark()
doc = """ <Placemark>
<name>Document Feature 1</name>
<styleUrl>#exampleStyleDocument</styleUrl>
<Point>
<coordinates>-122.371,37.816,0</coordinates>
</Point>
</Placemark>"""
Post by Sean Gillies
pm.from_string(doc)
afaik simplekml can write kml files but not read them (correct me if I
am wrong).

fastkml just supports a subset of what simplekml can write (but this is
of course only a first alpha). I doubt that fastkml will implement the whole
kml and gx standard. I just add new features as I go along and need them.
Thanks, and congratulations on this first release
steko
-----BEGIN PGP SIGNATURE-----
--
Best Regards,

Christian Ledermann

Nairobi - Kenya
Mobile : +254 702978914

<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don?t drive species to extinction

2) Don?t destroy a habitat that species rely on.

3) Don?t change the climate in ways that will result in the above.

}<(((*>
Christian Ledermann
2012-07-27 13:16:15 UTC
Permalink
On Thu, Jun 28, 2012 at 8:27 AM, Christian Ledermann
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Post by Sean Gillies
This would be a more general and less Shapely-dependent way to
represent geometries, if you like.
Especially considered that the library is otherwise pure Python, I
would second Sean's suggestion. I wonder if the "geojson" package fits
the purpose
I have nothing against Shapely of course, but its dependency on GEOS
makes it more complicated to install with modern Python packaging
tools like pip.
I just pushed v 0.2 to pypi which does not rely on shapely anymore (but will
happily use if it is installed)
--
Best Regards,

Christian Ledermann

Nairobi - Kenya
Mobile : +254 702978914

<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don?t drive species to extinction

2) Don?t destroy a habitat that species rely on.

3) Don?t change the climate in ways that will result in the above.

}<(((*>
Robert Sanson
2014-04-02 00:43:38 UTC
Permalink
Hi

Does anyone have some Python code for splitting an input line with another intersecting cut line into two separate lines?

Many thanks,

Robert Sanson



This email and any attachments are confidential and intended solely for the addressee(s). If you are not the intended recipient, please notify us immediately and then delete this email from your system.

This message has been scanned for Malware and Viruses by Websense Hosted Security.
www.websense.com
manel.clos
2014-04-02 07:21:43 UTC
Permalink
Hi Robert,

I've been doing some polygon cutting with lines that crosses them in PostGIS:

SELECT (ST_Dump(ST_Difference(poly, ST_Buffer(mlines, 0.0001)))).geom ....

Not python at all, but might help you get into some direction.

Hope it helps,

Manel Clos


-----Mensaje original-----
De: community-bounces at lists.gispython.org [mailto:community-bounces at lists.gispython.org] En nombre de Robert Sanson
Enviado el: mi?rcoles, 02 de abril de 2014 2:44
Para: gispython.org community projects
Asunto: [Community] Code for splitting a line with another line

Hi

Does anyone have some Python code for splitting an input line with another intersecting cut line into two separate lines?

Many thanks,

Robert Sanson



This email and any attachments are confidential and intended solely for the addressee(s). If you are not the intended recipient, please notify us immediately and then delete this email from your system.

This message has been scanned for Malware and Viruses by Websense Hosted Security.
www.websense.com
Harasty, Daniel J
2014-04-02 13:15:22 UTC
Permalink
Robert,



This will give you all the cut-up parts:


from shapely.geometry import LineString
from shapely.ops import cascaded_union

ls1 = LineString([(0,0), (10,10)])
ls2 = LineString([(10,0), (0,10)])

cu = cascaded_union([ls1, ls2])

print "cut-up lines:"
for item in cu.geoms:
print item



Note that if you consider ls2 the "cut line", and you *don't* want it in the output, you'll have to do a bit more work:

print "cut-up lines excluding the cut line:"
for item in cu.geoms:
if not ls2.contains(item):
print item


Hope that helps!



DJH



-----Original Message-----
From: community-bounces at lists.gispython.org [mailto:community-bounces at lists.gispython.org] On Behalf Of Robert Sanson
Sent: Tuesday, April 01, 2014 8:44 PM
To: gispython.org community projects
Subject: [Community] Code for splitting a line with another line



Hi



Does anyone have some Python code for splitting an input line with another intersecting cut line into two separate lines?



Many thanks,



Robert Sanson







This email and any attachments are confidential and intended solely for the addressee(s). If you are not the intended recipient, please notify us immediately and then delete this email from your system.



This message has been scanned for Malware and Viruses by Websense Hosted Security.

www.websense.com<http://www.websense.com>

_______________________________________________

Community mailing list

Community at lists.gispython.org<mailto:Community at lists.gispython.org>

http://lists.gispython.org/mailman/listinfo/community
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20140402/4f201574/attachment.htm>
Sean Gillies
2014-04-02 15:53:27 UTC
Permalink
DJH,

That's a clever approach. I was going to suggest finding the intersection
of the lines and then breaking the input strings coords into two parts, but
I think yours is more direct
Post by manel.clos
Robert,
from shapely.geometry import LineString
from shapely.ops import cascaded_union
ls1 = LineString([(0,0), (10,10)])
ls2 = LineString([(10,0), (0,10)])
cu = cascaded_union([ls1, ls2])
print *"cut-up lines:"*
print item
Note that if you consider ls2 the "cut line", and you **don't** want it
print *"cut-up lines excluding the cut line:"*
print item
Hope that helps!
DJH
-----Original Message-----
community-bounces at lists.gispython.org] On Behalf Of Robert Sanson
Sent: Tuesday, April 01, 2014 8:44 PM
To: gispython.org community projects
Subject: [Community] Code for splitting a line with another line
Hi
Does anyone have some Python code for splitting an input line with another
intersecting cut line into two separate lines?
Many thanks,
Robert Sanson
This email and any attachments are confidential and intended solely for
the addressee(s). If you are not the intended recipient, please notify us
immediately and then delete this email from your system.
This message has been scanned for Malware and Viruses by Websense Hosted Security.
www.websense.com
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20140402/40d5d1eb/attachment-0001.htm>
Robert Sanson
2014-04-02 22:18:36 UTC
Permalink
Hi Daniel

Thanks for the suggestion. I wanted to exclude the cutline but I found that " if not ls2.contains(item):" was not always reliable for complex linestrings so this is what I have done:

from shapely.geometry import LineString
from shapely.ops import cascaded_union

ls1 = LineString([(0,0), (10,10)])
ls2 = LineString([(10,0), (0,10)])
ls2p = ls2.buffer(0.1)

cu = cascaded_union([ls1, ls2])

for item in cu.geoms:
if not item.within(ls2p):
print item

Cheers,

Robert
"Harasty, Daniel J" <dharasty at appcomsci.com> 3/04/2014 2:15 a.m. >>>
Robert,



This will give you all the cut-up parts:


from shapely.geometry import LineString
from shapely.ops import cascaded_union

ls1 = LineString([(0,0), (10,10)])
ls2 = LineString([(10,0), (0,10)])

cu = cascaded_union([ls1, ls2])

print "cut-up lines:"
for item in cu.geoms:
print item



Note that if you consider ls2 the "cut line", and you *don't* want it in the output, you'll have to do a bit more work:

print "cut-up lines excluding the cut line:"
for item in cu.geoms:
if not ls2.contains(item):
print item


Hope that helps!



DJH



-----Original Message-----
From: community-bounces at lists.gispython.org [mailto:community-bounces at lists.gispython.org] On Behalf Of Robert Sanson
Sent: Tuesday, April 01, 2014 8:44 PM
To: gispython.org community projects
Subject: [Community] Code for splitting a line with another line



Hi



Does anyone have some Python code for splitting an input line with another intersecting cut line into two separate lines?



Many thanks,



Robert Sanson







This email and any attachments are confidential and intended solely for the addressee(s). If you are not the intended recipient, please notify us immediately and then delete this email from your system.



This message has been scanned for Malware and Viruses by Websense Hosted Security.

www.websense.com<http://www.websense.com>

_______________________________________________

Community mailing list

Community at lists.gispython.org<mailto:Community at lists.gispython.org>

http://lists.gispython.org/mailman/listinfo/community

Loading...