Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fastkml
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Aurélien Vermylen
fastkml
Commits
8494df50
Commit
8494df50
authored
Jun 27, 2012
by
Christian Ledermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enable prettyprint, add usage examples to README
parent
562c8677
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
166 additions
and
4 deletions
+166
-4
README.rst
README.rst
+152
-0
fastkml/atom.py
fastkml/atom.py
+8
-3
fastkml/kml.py
fastkml/kml.py
+5
-1
fastkml/tests.py
fastkml/tests.py
+1
-0
No files found.
README.rst
View file @
8494df50
...
@@ -22,3 +22,155 @@ provides (Point, LineString, Polygon, MultiPoint, MultiLineString,
...
@@ -22,3 +22,155 @@ provides (Point, LineString, Polygon, MultiPoint, MultiLineString,
MultiPolygon and LinearRing). While KML allows for more abstract
MultiPolygon and LinearRing). While KML allows for more abstract
MultiGeometries consisting of a combination of Points, LineStrings
MultiGeometries consisting of a combination of Points, LineStrings
and LinearRings, this is not supported in fastkml
and LinearRings, this is not supported in fastkml
Usage
=====
You can find more examples in the included tests.py file, here is a
quick overview:
Build a KML from scatch:
------------------------
::
>>> 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
----------------
You can create a KML object by reading 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>
fastkml/atom.py
View file @
8494df50
...
@@ -27,6 +27,10 @@ except ImportError:
...
@@ -27,6 +27,10 @@ except ImportError:
import
xml.etree.ElementTree
as
etree
import
xml.etree.ElementTree
as
etree
LXML
=
False
LXML
=
False
import
re
regex
=
r"^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\
.)*[
a-zA-Z]{2,4}$"
check_email
=
re
.
compile
(
regex
).
match
class
Link
(
object
):
class
Link
(
object
):
"""
"""
...
@@ -159,9 +163,9 @@ class _Person(object):
...
@@ -159,9 +163,9 @@ class _Person(object):
uri
=
etree
.
SubElement
(
element
,
"%suri"
%
self
.
ns
)
uri
=
etree
.
SubElement
(
element
,
"%suri"
%
self
.
ns
)
uri
.
text
=
self
.
uri
uri
.
text
=
self
.
uri
if
self
.
email
:
if
self
.
email
:
#XXX validate email
email
=
etree
.
SubElement
(
element
,
"%semail"
%
self
.
ns
)
email
=
etree
.
SubElement
(
element
,
"%semail"
%
self
.
ns
)
email
.
text
=
self
.
email
if
check_email
(
self
.
email
):
email
.
text
=
self
.
email
...
@@ -180,7 +184,8 @@ class _Person(object):
...
@@ -180,7 +184,8 @@ class _Person(object):
self
.
uri
=
uri
.
text
self
.
uri
=
uri
.
text
email
=
element
.
find
(
'%semail'
%
self
.
ns
)
email
=
element
.
find
(
'%semail'
%
self
.
ns
)
if
email
is
not
None
:
if
email
is
not
None
:
self
.
email
=
email
.
text
if
check_email
(
email
):
self
.
email
=
email
.
text
class
Author
(
_Person
):
class
Author
(
_Person
):
...
...
fastkml/kml.py
View file @
8494df50
...
@@ -76,7 +76,11 @@ class KML(object):
...
@@ -76,7 +76,11 @@ class KML(object):
def
to_string
(
self
,
prettyprint
=
False
):
def
to_string
(
self
,
prettyprint
=
False
):
""" Returm the KML Object as xml """
""" Returm the KML Object as xml """
return
etree
.
tostring
(
self
.
etree_element
(),
encoding
=
'utf-8'
)
if
LXML
and
prettyprint
:
return
etree
.
tostring
(
self
.
etree_element
(),
encoding
=
'utf-8'
,
pretty_print
=
True
)
else
:
return
etree
.
tostring
(
self
.
etree_element
(),
encoding
=
'utf-8'
)
def
features
(
self
):
def
features
(
self
):
""" return a list of features """
""" return a list of features """
...
...
fastkml/tests.py
View file @
8494df50
...
@@ -408,6 +408,7 @@ class StyleFromStringTestCase( unittest.TestCase ):
...
@@ -408,6 +408,7 @@ class StyleFromStringTestCase( unittest.TestCase ):
</Style>
</Style>
</Document>
</Document>
</kml>"""
</kml>"""
#XXX fil and outline
k
=
kml
.
KML
()
k
=
kml
.
KML
()
k
.
from_string
(
doc
)
k
.
from_string
(
doc
)
self
.
assertEqual
(
len
(
k
.
features
()),
1
)
self
.
assertEqual
(
len
(
k
.
features
()),
1
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment