geography : some minor tweaks for kml and prepare for feature request 08428
This commit is contained in:
parent
588e2f9427
commit
7afda11c9b
@ -54,6 +54,7 @@ from gi.repository import Pango, PangoCairo
|
|||||||
# Gramps Modules
|
# Gramps Modules
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
|
from .libkml import Kml
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -72,15 +73,14 @@ class KmlLayer(GObject.GObject, osmgpsmap.MapLayer):
|
|||||||
* Allowed : points, paths and polygons.
|
* Allowed : points, paths and polygons.
|
||||||
|
|
||||||
* One point : name, (latitude, longitude)
|
* One point : name, (latitude, longitude)
|
||||||
* One path : name, [ (latitude, longitude), (latitude, longitude), ...]
|
* One path : name, type, color, transparency, [ (latitude, longitude), (latitude, longitude), ...]
|
||||||
* One polygon : name, fill, [ (latitude, longitude), (latitude, longitude), ...]
|
* One polygon : name, type, color, transparency, [ (latitude, longitude), (latitude, longitude), ...]
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Initialize the layer
|
Initialize the layer
|
||||||
"""
|
"""
|
||||||
GObject.GObject.__init__(self)
|
GObject.GObject.__init__(self)
|
||||||
self.dots = []
|
|
||||||
self.paths = []
|
self.paths = []
|
||||||
self.polygons = []
|
self.polygons = []
|
||||||
self.tag = ""
|
self.tag = ""
|
||||||
@ -92,111 +92,31 @@ class KmlLayer(GObject.GObject, osmgpsmap.MapLayer):
|
|||||||
"""
|
"""
|
||||||
reset the layer attributes.
|
reset the layer attributes.
|
||||||
"""
|
"""
|
||||||
self.dots = []
|
|
||||||
self.paths = []
|
self.paths = []
|
||||||
self.polygons = []
|
self.polygons = []
|
||||||
self.name = ""
|
self.name = ""
|
||||||
|
|
||||||
def polygon(self, attributes):
|
|
||||||
self.points = []
|
|
||||||
for subAttribute in attributes:
|
|
||||||
if subAttribute.tag == self.tag + 'outerBoundaryIs':
|
|
||||||
for subsubAttribute in subAttribute:
|
|
||||||
if subsubAttribute.tag == self.tag + 'LinearRing':
|
|
||||||
for subsubsubAttribute in subsubAttribute:
|
|
||||||
if subsubsubAttribute.tag == self.tag + 'coordinates':
|
|
||||||
for point in subsubsubAttribute.text.split():
|
|
||||||
try:
|
|
||||||
(longitude, latitude, altitude) = point.split(',')
|
|
||||||
except:
|
|
||||||
(longitude, latitude) = point.split(',')
|
|
||||||
self.points.append((float(latitude), float(longitude)))
|
|
||||||
if subAttribute.tag == self.tag + 'innerBoundaryIs':
|
|
||||||
for subsubAttribute in subAttribute:
|
|
||||||
if subsubAttribute.tag == self.tag + 'LinearRing':
|
|
||||||
for subsubsubAttribute in subsubAttribute:
|
|
||||||
if subsubsubAttribute.tag == self.tag + 'coordinates':
|
|
||||||
for point in subsubsubAttribute.text.split():
|
|
||||||
try:
|
|
||||||
(longitude, latitude, altitude) = point.split(',')
|
|
||||||
except:
|
|
||||||
(longitude, latitude) = point.split(',')
|
|
||||||
self.points.append((float(latitude), float(longitude)))
|
|
||||||
if subAttribute.tag == self.tag + 'LinearRing':
|
|
||||||
for subsubAttribute in subAttribute:
|
|
||||||
if subsubAttribute.tag == self.tag + 'coordinates':
|
|
||||||
for point in subsubAttribute.text.split():
|
|
||||||
try:
|
|
||||||
(longitude, latitude, altitude) = point.split(',')
|
|
||||||
except:
|
|
||||||
(longitude, latitude) = point.split(',')
|
|
||||||
self.points.append((float(latitude), float(longitude)))
|
|
||||||
|
|
||||||
def add_kml(self, kml_file):
|
def add_kml(self, kml_file):
|
||||||
"""
|
"""
|
||||||
Add a kml file.
|
Add a kml file.
|
||||||
The access right and validity must be verified before this method.
|
The access right and validity must be verified before this method.
|
||||||
"""
|
"""
|
||||||
tree = ETree.parse(kml_file)
|
self.kml = Kml(kml_file)
|
||||||
root = tree.getroot()
|
(paths, polygons) = self.kml.add_kml()
|
||||||
self.tag = root.tag.replace('}kml','}')
|
if paths != []:
|
||||||
_LOG.debug("Tag version of kml file %s is %s" % (kml_file, self.tag))
|
self.paths.append(paths)
|
||||||
fname, extension = os.path.splitext(kml_file)
|
if polygons != []:
|
||||||
fdir, kmlfile = os.path.split(fname)
|
self.polygons.append(polygons)
|
||||||
self.index = -1
|
|
||||||
lineStrings = tree.findall('.//' + self.tag + 'Placemark')
|
|
||||||
for attributes in lineStrings:
|
|
||||||
for subAttribute in attributes:
|
|
||||||
if subAttribute.tag == self.tag + 'name':
|
|
||||||
self.name = subAttribute.text
|
|
||||||
if subAttribute.tag == self.tag + 'Polygon':
|
|
||||||
self.type = 'Polygon'
|
|
||||||
self.points = []
|
|
||||||
self.polygon(subAttribute)
|
|
||||||
if self.name == "":
|
|
||||||
self.polygons.append((kmlfile, self.points))
|
|
||||||
else:
|
|
||||||
self.polygons.append((self.name, self.points))
|
|
||||||
if subAttribute.tag == self.tag + 'Point':
|
|
||||||
self.type = 'Point'
|
|
||||||
self.points = []
|
|
||||||
for subsubAttribute in subAttribute:
|
|
||||||
if subsubAttribute.tag == self.tag + 'coordinates':
|
|
||||||
for point in subsubAttribute.text.split():
|
|
||||||
try:
|
|
||||||
(longitude, latitude, altitude) = point.split(',')
|
|
||||||
except:
|
|
||||||
(longitude, latitude) = point.split(',')
|
|
||||||
self.points.append((float(latitude), float(longitude)))
|
|
||||||
if self.name == "":
|
|
||||||
self.dots.append((kmlfile, self.points))
|
|
||||||
else:
|
|
||||||
self.dots.append((self.name, self.points))
|
|
||||||
if subAttribute.tag == self.tag + 'LineString':
|
|
||||||
self.type = 'Path'
|
|
||||||
self.points = []
|
|
||||||
for subsubAttribute in subAttribute:
|
|
||||||
if subsubAttribute.tag == self.tag + 'coordinates':
|
|
||||||
for point in subsubAttribute.text.split():
|
|
||||||
try:
|
|
||||||
(longitude, latitude, altitude) = point.split(',')
|
|
||||||
except:
|
|
||||||
(longitude, latitude) = point.split(',')
|
|
||||||
self.points.append((float(latitude), float(longitude)))
|
|
||||||
if self.name == "":
|
|
||||||
self.paths.append((kmlfile, self.points))
|
|
||||||
else:
|
|
||||||
self.paths.append((self.name, self.points))
|
|
||||||
|
|
||||||
def do_draw(self, gpsmap, ctx):
|
def do_draw(self, gpsmap, ctx):
|
||||||
"""
|
"""
|
||||||
Draw all the messages
|
Draw all the surfaces and paths
|
||||||
"""
|
"""
|
||||||
color1 = Gdk.color_parse('red')
|
color1 = Gdk.color_parse('red')
|
||||||
color2 = Gdk.color_parse('blue')
|
color2 = Gdk.color_parse('blue')
|
||||||
# We don't display self.dots. use markers.
|
for polygons in self.polygons:
|
||||||
for polygon in self.polygons:
|
for polygon in polygons:
|
||||||
(name, points) = polygon
|
(name, ptype, color, transparency, points) = polygon
|
||||||
map_points = []
|
map_points = []
|
||||||
for point in points:
|
for point in points:
|
||||||
conv_pt = osmgpsmap.MapPoint.new_degrees(point[0], point[1])
|
conv_pt = osmgpsmap.MapPoint.new_degrees(point[0], point[1])
|
||||||
@ -219,10 +139,18 @@ class KmlLayer(GObject.GObject, osmgpsmap.MapLayer):
|
|||||||
else:
|
else:
|
||||||
ctx.line_to(map_points[idx_pt][0], map_points[idx_pt][1])
|
ctx.line_to(map_points[idx_pt][0], map_points[idx_pt][1])
|
||||||
ctx.close_path()
|
ctx.close_path()
|
||||||
|
if ptype == "Polygon":
|
||||||
|
ctx.stroke()
|
||||||
|
if ptype == "OuterPolygon":
|
||||||
|
ctx.fill()
|
||||||
|
if ptype == "InnerPolygon":
|
||||||
|
ctx.set_source_rgba(1.0, 1.0, 1.0, 0.3)
|
||||||
|
ctx.set_operator(cairo.OPERATOR_ADD)
|
||||||
ctx.fill()
|
ctx.fill()
|
||||||
ctx.restore()
|
ctx.restore()
|
||||||
for path in self.paths:
|
for paths in self.paths:
|
||||||
(name, points) = path
|
for path in paths:
|
||||||
|
(name, ptype, color, transparency, points) = path
|
||||||
map_points = []
|
map_points = []
|
||||||
for point in points:
|
for point in points:
|
||||||
conv_pt = osmgpsmap.MapPoint.new_degrees(point[0], point[1])
|
conv_pt = osmgpsmap.MapPoint.new_degrees(point[0], point[1])
|
||||||
|
Loading…
Reference in New Issue
Block a user