-
Notifications
You must be signed in to change notification settings - Fork 0
/
changeset2pg.py
152 lines (146 loc) · 6.1 KB
/
changeset2pg.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import psycopg2
def download_xml(min_lon,min_lat,max_lon,max_lat,extas):
import urllib
min_lon,min_lat,max_lon,max_lat
url='http://www.openstreetmap.org/api/0.6/changesets?%s,%s,%s,%s'%(min_lon,min_lat,max_lon,max_lat)
for key in extras.keys():
if key=='user':
url+=('?user=%s'%extras[key])
elif key=='uid' or key=='user':
url+=('?user=%s'%extras[key])
elif key=='time':
url+=('?time=%s'%extras[key])
elif key=='time-range':
url+=('?time=%s,%s'%(extras[key][0],extras[key][1]))
elif key=='open':
if extras[key]:
url+='?open=ture'
else:
url+='?open=false'
elif key=='closed':
if extras[key]:
url+='?closed=ture'
else:
url+='?closed=false'
elif key=='changesets':
url+='?changesets='
for element in extras[key]:
url+=str(element)+","
if url[-1]==',':
url=url[:-1]
response=urllib.urlopen(url)
xml = response.read()
return xml
def create_table(host,db_username,db_password,database):
conn = psycopg2.connect("dbname=%s user=%s password=%s host=%s"%(database,db_username,db_password,host))
cur = conn.cursor()
sql='CREATE TABLE changeset(id integer NOT NULL,"user" text NOT NULL,uid integer NOT NULL,created_at timestamp with time zone,closed_at timestamp with time zone,open boolean,area geometry,CONSTRAINT index PRIMARY KEY (id))WITH (OIDS=FALSE);'
print sql
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
def import_data(database,db_username,db_password,host,xml):
from lxml import etree
conn = psycopg2.connect("dbname=%s user=%s password=%s host=%s"%(database,db_username,db_password,host))
cur = conn.cursor()
data=etree.fromstring(xml)
for change in data:
sql="INSERT INTO changeset ("
sql_values=" VALUES( "
temp=(set(change.attrib.keys())-set(['min_lat','max_lat','min_lon','max_lon']))
k=[]
for key in temp:
sql+='"'+str(key)+'",'
if key=='id':
sql_values+=str(change.attrib[key])+","
elif key=='user':
sql_values+=psycopg2.extensions.QuotedString(change.attrib[key].encode('utf-8')).getquoted()+","
elif key=='uid':
sql_values+=str(change.attrib[key])+","
elif key=='created_at':
sql_values+=psycopg2.extensions.QuotedString(change.attrib[key]).getquoted()+","
elif key=='closed_at':
sql_values+=psycopg2.extensions.QuotedString(change.attrib[key]).getquoted()+","
elif key=='open':
if str(change.attrib['open'])=='true':
sql_values+="'t',"
else:
sql_values+="'f',"
if 'min_lat' in change.attrib and 'max_lat' in change.attrib and 'min_lon' in change.attrib and 'max_lon' in change.attrib:
sql_values+="ST_GeomFromText('POLYGON(("+str(change.attrib['min_lat'])+" "+str(change.attrib['min_lon'])+","+str(change.attrib['max_lat'])+" "+str(change.attrib['min_lon'])+","+str(change.attrib['min_lat'])+" "+str(change.attrib['max_lon'])+","+str(change.attrib['max_lat'])+" "+str(change.attrib['max_lon'])+","+str(change.attrib['min_lat'])+" "+str(change.attrib['min_lon'])+"))')"
sql+="area,"
while sql[-1]==',':
sql=sql[:-1]
while sql_values[-1]==',':
sql_values=sql_values[:-1]
sql+=")"
sql_values+=");"
sql+=sql_values
print sql
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
def print_help():
print "Imports chagesets of an area to Postgresql database"
print "Syntax:changeset2pg <host> <db username> <db password> <database> <min_lat> <max_lat> <min_lon> <max_lon>"
print "Extra arguments:"
print "-user <username>"
print "-time <start time> (YYYY-MM-DD)"
print "-time-range <starttime> <end time>"
print "-open <true/false>"
print "-closed <true/false>"
print "-changesets <changeset1,changeset2,ect>"
exit()
if __name__ == '__main__':
if len(sys.argv)<9:
print_help()
else:
host=sys.argv[1]
db_username=sys.argv[2]
db_password=sys.argv[3]
database=sys.argv[4]
min_lon=sys.argv[5]
min_lat=sys.argv[6]
max_lon=sys.argv[7]
max_lat=sys.argv[8]
x=9
extras={}
while(x<len(sys.argv)):
if sys.argv[x]=='-user'and len(sys.argv)>=(x+1):
extras['user']=sys.argv[x+1]
x+=1
elif sys.argv[x]=='-time' and len(sys.argv)>=(x+1):
extras['time']=sys.argv[x+1]
x+=1
elif sys.argv[x]=='-time-range'and len(sys.argv)>=(x+2):
extras['time-range']=[sys.argv[x+1],sys.argv[x+2]]
x+=2
elif sys.argv[x]=='-open'and len(sys.argv)>=(x+1) :
extras['open']=sys.argv[x+1]
x+=1
elif sys.argv[x]=='-closed'and len(sys.argv)>=(x+1) :
if sys.argv[x+1]=='true':
extras['']=True
x+=1
elif sys.argv[x+1]=='false':
extras['closed']=False
x+=1
elif sys.argv[x]=='-changesets' and len(sys.argv)>=(x+2):
x+=1
extras['changesets']=[sys.argv[x]]
while sys.argv[x] not in ['user','time','time-range','open','closed']:
extras['changesets'].append(sys.argv[x])
x+=1
else:
print sys.argv[x]
print_help()
x+=1
xml=download_xml(min_lon,min_lat,max_lon,max_lat,extras)
#xml=download_xml(41.9737,2.7753,2.9101,42.0351,extras)
create_table(host,db_username,db_password,database)
import_data(database,db_username,db_password,host,xml)