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
1951be6b
Commit
1951be6b
authored
Nov 12, 2012
by
Christian Ledermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests for geometry.Geometry
parent
d2f9d77c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
169 additions
and
11 deletions
+169
-11
fastkml/geometry.py
fastkml/geometry.py
+36
-5
fastkml/kml.py
fastkml/kml.py
+1
-1
fastkml/tests.py
fastkml/tests.py
+132
-5
No files found.
fastkml/geometry.py
View file @
1951be6b
...
...
@@ -54,7 +54,7 @@ class Geometry(_BaseObject):
geometry: a geometry that implements the __geo_interface__ convention
extrude: boolean --> Specifies whether to connect the feature to
the ground with a line. To extrude a
Point
, the value for
the ground with a line. To extrude a
Feature
, the value for
'altitudeMode' must be either relativeToGround, relativeToSeaFloor,
or absolute. The feature is extruded toward the center of the
Earth's sphere.
...
...
@@ -107,6 +107,21 @@ class Geometry(_BaseObject):
self
.
geometry
=
asShape
(
geometry
)
def
_set_altitude_mode
(
self
,
element
):
if
self
.
altitude_mode
:
assert
(
self
.
altitude_mode
in
[
'clampToGround'
,
#'relativeToSeaFloor', 'clampToSeaFloor',
'relativeToGround'
,
'absolute'
])
if
self
.
altitude_mode
!=
'clampToGround'
:
element
.
set
(
'altitudeMode'
,
self
.
altitude_mode
)
def
_set_extrude
(
self
,
element
):
if
self
.
extrude
and
self
.
altitude_mode
in
[
'relativeToGround'
,
#'relativeToSeaFloor',
'absolute'
]:
element
.
set
(
'extrude'
,
'1'
)
def
_etree_coordinates
(
self
,
coordinates
):
clampToGround
=
(
self
.
altitude_mode
==
'clampToGround'
)
or
(
self
.
altitude_mode
==
None
)
element
=
etree
.
Element
(
"%scoordinates"
%
self
.
ns
)
...
...
@@ -129,24 +144,40 @@ class Geometry(_BaseObject):
def
_etree_point
(
self
,
point
):
element
=
etree
.
Element
(
"%sPoint"
%
self
.
ns
)
self
.
_set_extrude
(
element
)
self
.
_set_altitude_mode
(
element
)
coords
=
list
(
point
.
coords
)
element
.
append
(
self
.
_etree_coordinates
(
coords
))
return
element
def
_etree_linestring
(
self
,
linestring
):
element
=
etree
.
Element
(
"%sLineString"
%
self
.
ns
)
self
.
_set_extrude
(
element
)
self
.
_set_altitude_mode
(
element
)
if
self
.
tessellate
and
self
.
altitude_mode
in
[
'clampToGround'
,
'clampToSeaFloor'
]:
element
.
set
(
'tessellate'
,
'1'
)
coords
=
list
(
linestring
.
coords
)
element
.
append
(
self
.
_etree_coordinates
(
coords
))
return
element
def
_etree_linearring
(
self
,
linearring
):
element
=
etree
.
Element
(
"%sLinearRing"
%
self
.
ns
)
self
.
_set_extrude
(
element
)
self
.
_set_altitude_mode
(
element
)
# tesseleation is ignored by polygon and tesselation together with
# LinearRing without a polygon very rare Edgecase -> ignore for now
#if self.tessellate and self.altitude_mode in ['clampToGround',
# 'clampToSeaFloor']:
# element.set('tessellate', '1')
coords
=
list
(
linearring
.
coords
)
element
.
append
(
self
.
_etree_coordinates
(
coords
))
return
element
def
_etree_polygon
(
self
,
polygon
):
element
=
etree
.
Element
(
"%sPolygon"
%
self
.
ns
)
self
.
_set_extrude
(
element
)
self
.
_set_altitude_mode
(
element
)
outer_boundary
=
etree
.
SubElement
(
element
,
"%souterBoundaryIs"
%
self
.
ns
)
outer_boundary
.
append
(
self
.
_etree_linearring
(
polygon
.
exterior
))
for
ib
in
polygon
.
interiors
:
...
...
@@ -177,10 +208,10 @@ class Geometry(_BaseObject):
for
feature
in
features
.
geoms
:
if
isinstance
(
feature
,
Point
):
element
.
append
(
self
.
_etree_point
(
feature
))
elif
isinstance
(
feature
,
LineString
):
element
.
append
(
self
.
_etree_linestring
(
feature
))
elif
isinstance
(
feature
,
LinearRing
):
element
.
append
(
self
.
_etree_linearring
(
feature
))
elif
isinstance
(
feature
,
LineString
):
element
.
append
(
self
.
_etree_linestring
(
feature
))
elif
isinstance
(
feature
,
Polygon
):
element
.
append
(
self
.
_etree_polygon
(
feature
))
else
:
...
...
@@ -190,10 +221,10 @@ class Geometry(_BaseObject):
def
etree_element
(
self
):
if
isinstance
(
self
.
geometry
,
Point
):
return
self
.
_etree_point
(
self
.
geometry
)
elif
isinstance
(
self
.
geometry
,
LineString
):
return
self
.
_etree_linestring
(
self
.
geometry
)
elif
isinstance
(
self
.
geometry
,
LinearRing
):
return
self
.
_etree_linearring
(
self
.
geometry
)
elif
isinstance
(
self
.
geometry
,
LineString
):
return
self
.
_etree_linestring
(
self
.
geometry
)
elif
isinstance
(
self
.
geometry
,
Polygon
):
return
self
.
_etree_polygon
(
self
.
geometry
)
elif
isinstance
(
self
.
geometry
,
MultiPoint
):
...
...
fastkml/kml.py
View file @
1951be6b
...
...
@@ -640,7 +640,7 @@ class Placemark(_Feature):
else
:
logger
.
warn
(
'No geometries found'
)
#XXX move to geometry.Geometry
def
_etree_coordinates
(
self
,
coordinates
):
element
=
etree
.
Element
(
"%scoordinates"
%
self
.
ns
)
if
len
(
coordinates
[
0
])
==
2
:
...
...
fastkml/tests.py
View file @
1951be6b
...
...
@@ -891,7 +891,130 @@ class AtomTestCase( unittest.TestCase ):
l
.
href
=
None
self
.
assertRaises
(
ValueError
,
l
.
to_string
)
class
GeometryTestCase
(
unittest
.
TestCase
):
class
SetGeometryTestCase
(
unittest
.
TestCase
):
def
test_altitude_mode
(
self
):
geom
=
Geometry
()
geom
.
geometry
=
Point
(
0
,
1
)
self
.
assertEqual
(
geom
.
altitude_mode
,
None
)
self
.
assertFalse
(
'altitudeMode'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'unknown'
self
.
assertRaises
(
AssertionError
,
geom
.
to_string
)
geom
.
altitude_mode
=
'clampToSeaFloor'
self
.
assertRaises
(
AssertionError
,
geom
.
to_string
)
geom
.
altitude_mode
=
'relativeToSeaFloor'
self
.
assertRaises
(
AssertionError
,
geom
.
to_string
)
geom
.
altitude_mode
=
'clampToGround'
self
.
assertFalse
(
'altitudeMode'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'relativeToGround'
self
.
assertTrue
(
'altitudeMode="relativeToGround"'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'absolute'
self
.
assertTrue
(
'altitudeMode="absolute"'
in
geom
.
to_string
())
def
test_extrude
(
self
):
geom
=
Geometry
()
self
.
assertEqual
(
geom
.
extrude
,
False
)
geom
.
geometry
=
Point
(
0
,
1
)
geom
.
extrude
=
False
self
.
assertFalse
(
'extrude'
in
geom
.
to_string
())
geom
.
extrude
=
True
geom
.
altitude_mode
=
'clampToGround'
self
.
assertFalse
(
'extrude'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'relativeToGround'
self
.
assertTrue
(
'extrude="1"'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'absolute'
self
.
assertTrue
(
'extrude="1"'
in
geom
.
to_string
())
def
test_tesselate
(
self
):
geom
=
Geometry
()
self
.
assertEqual
(
geom
.
tessellate
,
False
)
geom
.
geometry
=
LineString
([(
0
,
0
),
(
1
,
1
)])
self
.
assertFalse
(
'tessellate'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'clampToGround'
self
.
assertFalse
(
'tessellate'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'relativeToGround'
self
.
assertFalse
(
'tessellate'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'absolute'
self
.
assertFalse
(
'tessellate'
in
geom
.
to_string
())
geom
.
tessellate
=
True
geom
.
altitude_mode
=
None
self
.
assertFalse
(
'tessellate'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'relativeToGround'
self
.
assertFalse
(
'tessellate'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'absolute'
self
.
assertFalse
(
'tessellate'
in
geom
.
to_string
())
geom
.
altitude_mode
=
'clampToGround'
self
.
assertTrue
(
'tessellate="1"'
in
geom
.
to_string
())
#for geometries != LineString tesselate is ignored
geom
.
geometry
=
Point
(
0
,
1
)
self
.
assertFalse
(
'tessellate'
in
geom
.
to_string
())
def
testPoint
(
self
):
p
=
Point
(
0
,
1
)
g
=
Geometry
(
geometry
=
p
)
self
.
assertEqual
(
g
.
geometry
,
p
)
g
=
Geometry
(
geometry
=
p
.
__geo_interface__
)
self
.
assertEqual
(
g
.
geometry
.
__geo_interface__
,
p
.
__geo_interface__
)
self
.
assertTrue
(
'Point'
in
g
.
to_string
())
self
.
assertTrue
(
'coordinates>0.000000,1.000000</'
in
g
.
to_string
())
def
testLineString
(
self
):
l
=
LineString
([(
0
,
0
),
(
1
,
1
)])
g
=
Geometry
(
geometry
=
l
)
self
.
assertEqual
(
g
.
geometry
,
l
)
self
.
assertTrue
(
'LineString'
in
g
.
to_string
())
self
.
assertTrue
(
'coordinates>0.000000,0.000000 1.000000,1.000000</'
in
g
.
to_string
())
def
testLinearRing
(
self
):
l
=
LinearRing
([(
0
,
0
),
(
1
,
0
),
(
1
,
1
),
(
0
,
0
)])
g
=
Geometry
(
geometry
=
l
)
self
.
assertEqual
(
g
.
geometry
,
l
)
self
.
assertTrue
(
'LinearRing'
in
g
.
to_string
())
self
.
assertTrue
(
'coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 0.000000,0.000000</'
in
g
.
to_string
())
def
testPolygon
(
self
):
#without holes
l
=
Polygon
([(
0
,
0
),
(
1
,
0
),
(
1
,
1
),
(
0
,
0
)])
g
=
Geometry
(
geometry
=
l
)
self
.
assertEqual
(
g
.
geometry
,
l
)
self
.
assertTrue
(
'Polygon'
in
g
.
to_string
())
self
.
assertTrue
(
'outerBoundaryIs'
in
g
.
to_string
())
self
.
assertFalse
(
'innerBoundaryIs'
in
g
.
to_string
())
self
.
assertTrue
(
'LinearRing'
in
g
.
to_string
())
self
.
assertTrue
(
'coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 0.000000,0.000000</'
in
g
.
to_string
())
#with holes
p
=
Polygon
([(
-
1
,
-
1
),
(
2
,
-
1
),
(
2
,
2
),
(
-
1
,
-
1
)],[[(
0
,
0
),
(
1
,
0
),
(
1
,
1
),
(
0
,
0
)]])
g
=
Geometry
(
geometry
=
p
)
self
.
assertEqual
(
g
.
geometry
,
p
)
self
.
assertTrue
(
'Polygon'
in
g
.
to_string
())
self
.
assertTrue
(
'outerBoundaryIs'
in
g
.
to_string
())
self
.
assertTrue
(
'innerBoundaryIs'
in
g
.
to_string
())
self
.
assertTrue
(
'LinearRing'
in
g
.
to_string
())
self
.
assertTrue
(
'coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 0.000000,0.000000</'
in
g
.
to_string
())
self
.
assertTrue
(
'coordinates>-1.000000,-1.000000 2.000000,-1.000000 2.000000,2.000000 -1.000000,-1.000000</'
in
g
.
to_string
())
def
testMultiPoint
(
self
):
pass
def
testMultiLineString
(
self
):
pass
def
testMultiPolygon
(
self
):
pass
def
testGeometryCollection
(
self
):
pass
class
GetGeometryTestCase
(
unittest
.
TestCase
):
def
testPoint
(
self
):
pass
...
...
@@ -901,7 +1024,6 @@ class GeometryTestCase( unittest.TestCase ):
pass
def
testPolygon
(
self
):
pass
def
testMultiPoint
(
self
):
pass
def
testMultiLineString
(
self
):
...
...
@@ -916,7 +1038,6 @@ class GeometryTestCase( unittest.TestCase ):
class
Force3DTestCase
(
unittest
.
TestCase
):
def
test3d
(
self
):
#altitudeMode clampToGround indicates to ignore an altitude specification.
ns
=
''
p2
=
kml
.
Placemark
(
ns
,
'id'
,
'name'
,
'description'
)
p2
.
geometry
=
Polygon
([(
0
,
0
),
(
1
,
1
),
(
1
,
0
)])
...
...
@@ -926,7 +1047,12 @@ class Force3DTestCase( unittest.TestCase ):
self
.
assertNotEqual
(
p2
.
to_string
(),
p3
.
to_string
())
config
.
FORCE3D
=
True
self
.
assertEqual
(
p2
.
to_string
(),
p3
.
to_string
())
#XXX
#altitudeMode clampToGround indicates to ignore an altitude specification.
#p3.altitudeMode = 'clampToGround'
#self.assertEqual(p2.to_string(), p3.to_string())
#config.FORCE3D = False
#self.assertEqual(p2.to_string(), p3.to_string())
def
test_suite
():
suite
=
unittest
.
TestSuite
()
...
...
@@ -936,7 +1062,8 @@ def test_suite():
suite
.
addTest
(
unittest
.
makeSuite
(
BaseClassesTestCase
))
suite
.
addTest
(
unittest
.
makeSuite
(
DateTimeTestCase
))
suite
.
addTest
(
unittest
.
makeSuite
(
AtomTestCase
))
suite
.
addTest
(
unittest
.
makeSuite
(
GeometryTestCase
))
suite
.
addTest
(
unittest
.
makeSuite
(
SetGeometryTestCase
))
suite
.
addTest
(
unittest
.
makeSuite
(
GetGeometryTestCase
))
suite
.
addTest
(
unittest
.
makeSuite
(
Force3DTestCase
))
return
suite
...
...
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