-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_funct.py
114 lines (103 loc) · 3.7 KB
/
line_funct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
######################################################################
### info
# begin : 2010-05-10
# authors: Luca Delucchi
# copyright : (C) 2010 by luca delucchi, Fondazione Edmund Mach
# email : [email protected]
###
### license
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License (GPL) for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
###
### history
# 2010-08-27 create file for function interesting import line data
###
######################################################################
import general_funct as funct
def createListNodes(geom):
""" Return a list of nodes for line/polygon elements
geom = OGR geometry object
"""
# return geometry in WKT format
geomWKT = geom.ExportToWkt()
# if geometry is point
if geom.GetGeometryType() == 2:
# return a list of point (x,y)
listPoints = geomWKT.replace(')','').split('(')[1].split(',')
# if geometry is line
elif geom.GetGeometryType() == 3:
listPoints = geomWKT.replace('))','').split('((')[1].split(',')
# create list of nodes
listNodes = []
iD=-1
# for each point
for i in listPoints:
# create a list of x and y values
xy = i.split(' ')
# create node definition in osm format
nodeDef={unicode('id'):iD, unicode('lon'):i[0],unicode('lat'):i[1],
unicode('tag'):{}}
# add node to listnode
listNodes.append(nodeDef)
iD=iD-1
# if geometry type is polygon copy the first poi with the last
if geom.GetGeometryType() == 3:
listNodes[-1]=listNodes[0]
# return nodes list
return listNodes
def foundNearNode(geom,api):
""" Function to found the nearest node of a geometry
geom = geometry of first / last point
api = pythonOsmApi object
"""
osmData = funct.getMapData(geom,api,buf=0.0002)
osmNodes = funct.getPointData(osmData,line=True)
def correctEndNodes(listNodes,geom,api):
""" Function control if is present a way near the first and last point,
if it's present it connect the two lines
listNodes = element returned by createListNodes function
geom = the geometry of line
api = pythonOsmApi object
"""
nPoints = geom.GetPointCount()
# geometry of first point
geomStartNode = ogr.Geometry(ogr.wkbPoint)
geomStartNode.addpoint(geom.GetPoint(0))
# geometry of last point
geomEndNode = ogr.Geometry(ogr.wkbPoint)
geomEndNode.addpoint(geom.GetPoint(nPoints-1))
# function to found the near point
foundNearNode(geomStartNode,api)
foundNearNode(geomEndNode,api)
#startOsmData = funct.getMapData()
def loadLines(layer,api):
""" The function to load line and polygon vector inside OSM DB
layer = OGR layer object
api = PythonOsmApi object
"""
fields = funct.fieldsName(layer)
feature = layer.GetNextFeature()
listAllNodes = []
while feature:
geomFeat = feature.GetGeometryRef()
tags = funct.tagDef(feature,fields)
if geomFeat.GetGeometryType() == 3:
tags[unicode('area')] = unicode('yes')
listNodes = createListNodes(geomFeat)
correctEndNodes(listNodes,geomFeat,api)
wayDef = {unicode('nd'):listNodes, unicode('tag'):tags}
api.WayCreate(wayDef)
feature=layer.GetNextFeature()
api.flush()