source: subversion/applications/rendering/parking/osray/osray_ground.py @ 21972

Revision 21972, 5.0 KB checked in by kdrangmeister, 4 years ago (diff)

osray: db interface

Line 
1# -*- coding: utf-8 -*-
2# by kay - create color texture map for ground
3
4import sys
5import commands
6import psycopg2
7import csv
8import re
9from numpy import *
10
11def avg(a,b): return (a+b)/2.0
12
13landusetypes = {
14    'residential':['<0.9,1,0.9>'],
15    'commercial':['<1,1,0.9>'],
16    'hospital':['<1,0.8,0.8>'],
17    'landfill':['<0.6,0.7,0.4>'],
18    'recreation_ground':['<0.8,0.9,0.8>'],
19    'city_green':['<0.8,0.9,0.8>'],
20    'farm':['<0.8,0.9,0.8>'],
21    'farmland':['<0.8,0.9,0.8>'],
22    'wood':['<0.8,0.9,0.8>']
23    }
24
25def pov_landuse(f,landuse):
26    landusetype = landuse[1]
27    print "landusetype=",landusetype
28    #landusetype = 'secondary'
29    landuseparams = landusetypes.get(landusetype)
30
31    linestring = landuse[2]
32    linestring = linestring[8:] # cut off the "POLYGON("
33    linestring = linestring[:-1] # cut off the ")"
34
35    points = linestring.split(',')#.strip('(').strip(')')
36    #print points
37
38    numpoints = len(points)
39    f.write("prism {{ linear_spline  0, 0.01, {0},\n".format(numpoints))
40    f.write("/* osm_id={0} */\n".format(landuse[0]))
41
42    for i,point in enumerate(points):
43        latlon = point.split(' ')
44        if (i==0):
45            firstpoint="<{0}, {1}>\n".format(latlon[0],latlon[1])
46        if (i!=numpoints-1):
47            f.write("  <{0}, {1}>,\n".format(latlon[0].strip('(').strip(')'),latlon[1].strip('(').strip(')')))
48        else:
49            f.write("  <{0}, {1}>\n".format(latlon[0].strip('(').strip(')'),latlon[1].strip('(').strip(')')))
50    #f.write(firstpoint)
51
52    color = landuseparams[0]
53
54    f.write("""
55    texture {{
56        pigment {{
57            color rgb {0}
58        }}
59        finish {{
60            ambient 1
61            /*specular 0.5
62            roughness 0.05
63            reflection 0.5*/
64        }}
65    }}
66}}
67\n""".format(color))
68
69def pov_globals(f):
70    globsettings = """
71global_settings {{
72    assumed_gamma 1.5
73    noise_generator 2
74}}
75"""
76    f.write(globsettings.format())
77
78def pov_camera(f,bbox):
79    polygonstring = bbox[0][0]
80    polygonstring = polygonstring[9:] # cut off the "POLYGON(("
81    polygonstring = polygonstring[:-2] # cut off the "))"
82    #print polygonstring
83    points = polygonstring.split(',')
84
85    numpoints = len(points)
86
87    for i,point in enumerate(points):
88        latlon = point.split(' ')
89        if (i==0):
90            left=float(latlon[0])
91            bottom=float(latlon[1])
92        if (i==2):
93            right=float(latlon[0])
94            top=float(latlon[1])
95
96    #print bottom,left,top,right
97    zoom = 1.5
98    zoom = 1100.0/zoom
99    f.write("""
100camera {{
101   orthographic
102   location <0, 10000, 0>
103   sky <0, 1, 0>
104   direction <0, 0, 1>
105   right <1.3333*{2}, 0, 0>
106   up <0, 1*{2}*cos(radians({3})), 0>
107   look_at <0, 0, 0>
108   rotate <-{3},0,0>
109   scale <1,1,1>
110   translate <{0},0,{1}>
111}}
112""".format(avg(left,right),avg(bottom,top),zoom,10))
113    f.write("""
114/* ground */
115box {{
116    <{0}, -0.5, {1}>, <{2}, -0.0, {3}>
117    pigment {{
118        color rgb <1, 1, 1>
119    }}
120    finish {{
121        ambient 1
122    }}
123}}
124""".format(left,bottom,right,top))
125
126    #f.write("""light_source {{ <{0}, 50000, {1}>, rgb <0.5, 0.5, 0.5> }}\n""".format(avg(left*1.5,right*0.5),avg(bottom*1.5,top*0.5)))
127    #f.write("""light_source {{ <{0}, 5000, {1}>, rgb <0.5, 0.5, 0.5> }}\n""".format(avg(left*0.5,right*1.5),avg(bottom*1.5,top*0.5)))
128    #f.write("""light_source {{ <300000+{0}, 1000000, -1000000+{1}>, rgb <1, 1, 1> }}\n""".format(avg(left,right),avg(bottom,top)))
129
130def create_landuse_texture(conn,curs,options):
131    f_landuse = open('/home/kayd/workspace/Parking/osray/scene-osray-landuse-texture.pov', 'w')
132
133    thebbox = options['bbox']
134    prefix = options['prefix']
135
136    latlon= 'ST_Y(ST_Transform(ST_line_interpolate_point(way,0.5),4326)),ST_X(ST_Transform(ST_line_interpolate_point(way,0.5),4326))'
137    coords= "ST_Y(ST_line_interpolate_point(way,0.5)) as py,ST_X(ST_line_interpolate_point(way,0.5)) as px,ST_Y(ST_line_interpolate_point(way,0.49)) as qy,ST_X(ST_line_interpolate_point(way,0.49)) as qx,ST_Y(ST_line_interpolate_point(way,0.51)) as ry,ST_X(ST_line_interpolate_point(way,0.51)) as rx"
138    FlW = "FROM "+prefix+"_line WHERE"
139    FpW = "FROM "+prefix+"_polygon WHERE"
140
141    googbox = "transform(SetSRID('BOX3D("+thebbox+")'::box3d,4326),900913)"
142
143    #curs.execute("SELECT ST_AsText(transform(SetSRID('BOX3D(9.92498 49.78816,9.93955 49.8002)'::box3d,4326),900913)) AS geom")
144    curs.execute("SELECT ST_AsText("+googbox+") AS geom")
145
146    bbox = curs.fetchall()
147    pov_globals(f_landuse)
148    pov_camera(f_landuse,bbox)
149
150    landuses = []
151    for landusetype in landusetypes.iterkeys():
152        curs.execute("SELECT osm_id,landuse,ST_AsText(\"way\") AS geom "+FpW+" \"way\" && "+googbox+" and landuse='"+landusetype+"' LIMIT 1700;")
153        landuses += curs.fetchall()
154
155        for landuse in landuses:
156            pov_landuse(f_landuse,landuse)
157
158    f_landuse.close()
159
160    image_dimension_parameters = "-W"+str(options['width'])+" -H"+str(options['height'])
161    result = commands.getstatusoutput('povray -Iscene-osray-landuse-texture.pov -UV '+image_dimension_parameters+' +Q9 +A')
162    # print result[1]
163
Note: See TracBrowser for help on using the repository browser.