source: subversion/applications/rendering/parking/joinways/joinways.py @ 28283

Revision 28283, 5.2 KB checked in by kdrangmeister, 2 years ago (diff)

iterative approach

  • Property svn:mime-type set to text/plain
Line 
1# -*- coding: utf-8 -*-
2# by kay
3
4import sys
5import string
6#import psycopg2
7from osmdb import OSMDB
8from optparse import OptionParser
9
10class JoinDB (OSMDB):
11
12    def get_highways_segments(self):
13        #self.curs.execute("SELECT osm_id,"+latlon+",\"parking:condition:"+side+":maxstay\","+coords+",'"+side+"' "+FW+" \"parking:condition:"+side+":maxstay\" is not NULL and \"parking:condition:"+side+"\"='disc'")
14        result=[]
15        #self.curs.execute("select osm_id,name from planet_line where \"way\" && SetSRID('BOX3D(1101474.25471931 6406603.879863935,1114223.324055468 6415715.307134068)'::box3d, 900913)")
16        #print "result for bbox("+self.googbox+")"
17        #print "select osm_id,name "+self.FlW+" \"way\" && "+self.googbox+""
18        #self.curs.execute("select osm_id,name from planet_line where \"way\" && "+self.googbox+"")
19        self.curs.execute("select name,string_agg(text(osm_id),',') "+self.FlW+" highway is not Null and \"way\" && "+self.googbox+" and name is not Null group by name")
20        result += self.curs.fetchall()
21        highways=[]
22        for hw,osmids in result:
23            ids=osmids.split(',')
24            highways.append([hw,ids])
25
26    def get_next_pending_highway(self):
27        result=[]
28        # FIXME: bbox is to be removed later
29        print "select osm_id,highway,name,ST_AsText(\"way\") AS geom "+self.FlW+" jrhandled is False and highway is not Null and \"way\" && "+self.googbox+" and name is not Null limit 1"
30        self.curs.execute("select osm_id,highway,name,ST_AsText(\"way\") AS geom "+self.FlW+" jrhandled is False and highway is not Null and \"way\" && "+self.googbox+" and name is not Null limit 1")
31        result += self.curs.fetchall()
32        if len(result)==0:
33            raise BaseException("No pending highway found (this should not be an assert)")
34        res=result[0]
35        highway = {}
36        highway['osm_id']=res[0]
37        highway['highway']=res[1]
38        highway['name']=res[2]
39        highway['coords']=res[3]
40        return highway
41
42    def get_joined_ways(self,segment_ids):
43        result=[]
44        self.curs.execute("select st_linemerge(st_collect(way)) "+self.FlW+" osm_id in ("+string.join(segment_ids,',')+");")
45        result += self.curs.fetchall()
46        print "jw-result = "+str(result)
47        return result[0][0]
48
49    def insert_joined_highway(self,id,name,highway,way):
50        #self.curs.execute("SELECT osm_id,"+latlon+",\"parking:condition:"+side+":maxstay\","+coords+",'"+side+"' "+FW+" \"parking:condition:"+side+":maxstay\" is not NULL and \"parking:condition:"+side+"\"='disc'")
51        result=[]
52        #self.curs.execute("select osm_id,name from planet_line where \"way\" && SetSRID('BOX3D(1101474.25471931 6406603.879863935,1114223.324055468 6415715.307134068)'::box3d, 900913)")
53        print "insert into planet_line_join (join_id, name, highway, way) values ('"+id+"','"+name+"','"+highway+"',SetSrid('"+way+"'::Text,4326));"
54        self.curs.execute("insert into planet_line_join (join_id, name, highway, way) values ('"+id+"','"+name+"','"+highway+"',SetSrid('"+way+"'::Text,4326))")
55
56    def what_is_it(self,geom):
57        result=[]
58        self.curs.execute("select astext(setsrid('"+geom+"'::Text,4326))")
59        result += self.curs.fetchall()
60        itisa=result[0][0]
61        itisa=itisa.split('(')[0]
62        print "whatisit-result = "+itisa
63        return itisa
64       
65       
66
67"""
68'Kittelstra\xc3\x9fe', '36717484,36717485,5627159'
69
70create table planet_line_join (join_id integer , name text, highway text);
71select AddGeometryColumn('planet_line_join', 'way', 4326, 'LINESTRING', 2 );
72
73"""
74
75def main2(options):
76    bbox = options['bbox']
77    DSN = options['dsn']
78    print bbox
79    osmdb = JoinDB(DSN)
80    #highways = getHighwaysInBbox(DSN,bbox)
81    bxarray=bbox.split(",")
82    bbox="{b} {l},{t} {r}".format(b=bxarray[0],l=bxarray[1],t=bxarray[2],r=bxarray[3])
83    osmdb.set_bbox(bbox)
84    highways=osmdb.get_highways_segments()
85   
86    for hw in highways:
87        hwname = hw[0]
88        hwsegments = hw[1]
89        hwjoinedway = osmdb.get_joined_ways(hwsegments)
90        print "* Highway "+hwname+": "+hwjoinedway
91        itisa = osmdb.what_is_it(hwjoinedway)
92        if itisa=='LINESTRING':
93            osmdb.insert_joined_highway(str(0),hwname,"residential",hwjoinedway)
94        else:
95            print "not handled yet: "+hwname
96#        break
97
98def main(options):
99    bbox = options['bbox']
100    DSN = options['dsn']
101    print bbox
102    osmdb = JoinDB(DSN)
103    #highways = getHighwaysInBbox(DSN,bbox)
104    bxarray=bbox.split(",")
105    bbox="{b} {l},{t} {r}".format(b=bxarray[0],l=bxarray[1],t=bxarray[2],r=bxarray[3])
106    osmdb.set_bbox(bbox)
107    highway=osmdb.get_next_pending_highway()
108    print highway
109
110#        break
111
112if __name__ == '__main__':
113    parser = OptionParser()
114    parser.add_option("-c", "--command", dest="command", help="The command to execute. Default is update. Possible values are update, install, clear", default="update")
115    parser.add_option("-b", "--bbox", dest="bbox", help="bounding box to restrict to", default="")
116    parser.add_option("-d", "--dsn", dest="dsn", help="DSN, default is 'dbname=gis host=crite'", default="dbname=gis host=crite")
117    (options, args) = parser.parse_args()
118    print options
119    main(options.__dict__)
120    sys.exit(0)
Note: See TracBrowser for help on using the repository browser.