Commit 3c55c3bf authored by Rick Gruber-Riemer's avatar Rick Gruber-Riemer

Main changes from Python 2 to 3 after

* running 2to3
* doing manual conversions
* walk-through files to find compile errors
* fixes after running textures.manager, osm2city, osm2pylon, roads, piers and platforms
parent 081b3a5b
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import string
import matplotlib.pyplot as plt
import logging
from pdb import pm
import numpy as np
from pyparsing import Literal, Word, quotedString, alphas, Optional, OneOrMore, \
Group, ParseException, nums, Combine, Regex, alphanums, LineEnd, Each,\
ParserElement, ZeroOrMore
from pyparsing import Literal, Word, alphas, Optional, OneOrMore, \
Group, nums, Regex, alphanums, LineEnd, Each,\
ZeroOrMore
#fmt_node = '%1.6f'
fmt_node = '%g'
fmt_surf = '%1.4g'
class Node(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __str__(self):
return (fmt_node + ' ' + fmt_node + ' ' + fmt_node + '\n') % (self.x, self.y, self.z)
class Face(object):
"""if our texture is rotated in texture_atlas, set swap_uv=True"""
def __init__(self, nodes_uv_list, typ, mat, swap_uv):
......@@ -38,7 +37,7 @@ class Face(object):
s = "SURF %s\n" % self.typ
s += "mat %i\n" % self.mat
s += "refs %i\n" % len(self.nodes_uv_list)
s += string.join([("%i " + fmt_surf + " " + fmt_surf + "\n") % (n[0], n[1], n[2]) for n in self.nodes_uv_list], '')
s += "".join([("%i " + fmt_surf + " " + fmt_surf + "\n") % (n[0], n[1], n[2]) for n in self.nodes_uv_list])
return s
......@@ -112,26 +111,26 @@ backface. bit1 = shaded surface bit2 = twosided.
def __str__(self):
s = 'OBJECT poly\n'
if self.name != None:
if self.name is not None:
s += 'name "%s"\n' % self.name
if self.texrep != None:
if self.texrep is not None:
s += 'texrep %g %g\n' % (self.texrep[0], self.texrep[1])
if self.texoff != None:
if self.texoff is not None:
s += 'texoff %g %g\n' % (self.texoff[0], self.texoff[1])
if self.rot != None:
s += 'rot %s\n' % string.join(["%g" % item for item in self.rot])
if self.loc != None:
s += 'loc %g %g %g\n' % (self.loc[0], self.loc[1], self.loc[2])
if self.crease != None:
if self.rot is not None:
s += 'rot %s\n' % "".join(["%g" % item for item in self.rot])
if self.loc is not None:
s += 'loc %g %g %g\n' % (self.loc[0], self.loc[1], self.loc[2])
if self.crease is not None:
s += 'crease %g\n' % self.crease
if self.url != None:
if self.url is not None:
s += 'url %s\n' % self.url
if self.texture:
s += 'texture "%s"\n' %self.texture
s += 'texture "%s"\n' % self.texture
s += 'numvert %i\n' % len(self._nodes)
s += string.join([str(n) for n in self._nodes], '')
s += "".join([str(n) for n in self._nodes])
s += 'numsurf %i\n' % len(self._faces)
s += string.join([str(f) for f in self._faces], '')
s += "".join([str(f) for f in self._faces])
s += 'kids %i\n' % self.kids
return s
......@@ -152,6 +151,7 @@ backface. bit1 = shaded surface bit2 = twosided.
y = 0.5*(Y[i] + Y[i+1])
plt.text(x+Z[i], y+Z[i], "%i" % i)
class Label(Object):
def __init__(self):
super(Label, self).__init__('label', texture='tex/ascii.png')
......@@ -177,12 +177,13 @@ class Label(Object):
self.node(x, y, z+w)
self.node(x+h, y, z+w)
self.node(x+h, y, z)
self.face([(o, u0,v0),
(o+1,u1,v0),
(o+2,u1,v1),
(o+3,u0,v1)])
self.face([(o, u0, v0),
(o+1, u1, v0),
(o+2, u1, v1),
(o+3, u0, v1)])
z += w
class File(object):
"""
Hold a number of 3D objects, each object consiting of nodes and faces.
......@@ -193,7 +194,7 @@ class File(object):
a common source of bugs. Can also add 3d labels (useful for debugging, disabled
by default)
"""
def __init__(self, file_name = None, stats=None, show_labels = False):
def __init__(self, file_name=None, stats=None, show_labels=False):
"""Read ac3d data from file_name if given. Otherwise create empty ac3d object"""
self.objects = []
self.materials_list = []
......@@ -201,7 +202,7 @@ class File(object):
self.stats = stats
self._current_object = None
self.label_object = None
if file_name != None:
if file_name is not None:
self.read(file_name)
def new_object(self, name, texture, **kwargs):
......@@ -213,7 +214,8 @@ class File(object):
def add_label(self, text, x, y, z, orientation=0, scale=1.):
if not self.label_object:
self.label_object = Label()
if self.show_labels: self.objects.append(self.label_object)
if self.show_labels:
self.objects.append(self.label_object)
self.label_object.add(text, x, y, z, orientation, scale)
return self.label_object
......@@ -259,9 +261,10 @@ class File(object):
def nodes_as_array(self):
"""return all nodes as a numpy array"""
the_nodes = np.zeros((0,3))
the_nodes = np.zeros((0, 3))
for o in self.objects:
if o.is_empty(): continue
if o.is_empty():
continue
a = o.nodes_as_array()
the_nodes = np.vstack((the_nodes, a))
return the_nodes
......@@ -269,14 +272,14 @@ class File(object):
def __str__(self):
s = 'AC3Db\n'
if self.materials_list:
s += string.join(['MATERIAL %s\n' % the_mat for the_mat in self.materials_list], '')
s += "".join(['MATERIAL %s\n' % the_mat for the_mat in self.materials_list])
else:
s += 'MATERIAL "" rgb 1 1 1 amb 1 1 1 emis 0 0 0 spec 0.5 0.5 0.5 shi 64 trans 0\n'
# s += 'MATERIAL "" rgb 1 1 1 amb 0.5 0.5 0.5 emis 1 1 1 spec 0.5 0.5 0.5 shi 64 trans 0\n'
non_empty = [o for o in self.objects if not o.is_empty()]
# FIXME: this doesnt handle nested kids properly
s += 'OBJECT world\nkids %i\n' % len(non_empty)
s += string.join([str(o) for o in non_empty], '')
s += "".join([str(o) for o in non_empty])
return s
def write(self, file_name):
......@@ -300,7 +303,6 @@ class File(object):
self.new_object(None, None)
self._current_object._type = tokens[1]
def convertLKids(tokens):
self._current_object.kids = int(tokens[1])
......@@ -383,11 +385,11 @@ class File(object):
pObjectWorld = Group(lObject + Optional(lName) + lKids)
pSurf = (lSurf + Optional(lMat) + lRefs + Group(OneOrMore(lNodes))).setParseAction( convertSurf )
pObjectHeader = Group(lObject + Each([Optional(lName), Optional(lData), Optional(lTexture), Optional(lTexrep), \
Optional(lTexoff), Optional(lRot), Optional(lLoc), Optional(lUrl), Optional(lCrease)]))
pObject = Group(pObjectHeader + Optional(lNumvert + Group(ZeroOrMore(lVertex)) \
+ Optional(lNumsurf + Group(ZeroOrMore(pSurf)))) \
+ lKids)#.setParseAction( convertObject )
pObjectHeader = Group(lObject + Each([Optional(lName), Optional(lData), Optional(lTexture), Optional(lTexrep),
Optional(lTexoff), Optional(lRot), Optional(lLoc), Optional(lUrl), Optional(lCrease)]))
pObject = Group(pObjectHeader + Optional(lNumvert + Group(ZeroOrMore(lVertex))
+ Optional(lNumsurf + Group(ZeroOrMore(pSurf))))
+ lKids) # .setParseAction( convertObject )
# pObject.setDebug(True)
# pObject.debug = True
# ParserElement.verbose_stacktrace = True
......@@ -397,7 +399,7 @@ class File(object):
try:
self.p = pFile.parseFile(file_name)
except IOError, e:
except IOError as e:
logging.warning(e)
# todo:
......@@ -414,12 +416,12 @@ if __name__ == "__main__":
if 0:
a = File()
a.new_object('bla', '')
a.node(0,0,0)
a.node(0,0,1)
a.node(1,0,1)
a.node(1,0,0)
a.face([(0,0,0), (1,0,0), (2,0,0), (3,0,0)])
print a.total_faces(), a.total_nodes()
a.node(0, 0, 0)
a.node(0, 0, 1)
a.node(1, 0, 1)
a.node(1, 0, 0)
a.face([(0, 0, 0), (1, 0, 0), (2, 0, 0), (3, 0, 0)])
print(a.total_faces(), a.total_nodes())
nodes = a.nodes_as_array()
......@@ -443,23 +445,23 @@ if __name__ == "__main__":
nodes = -np.delete(a.nodes_as_array().transpose(), 1, 0)[::-1]
r = np.dot(Rot_mat, nodes)
print a.total_faces()
print a.total_nodes()
print(a.total_faces())
print(a.total_nodes())
if 1:
#plt.clf()
#a.plot()
plt.xlim(-50,50)
plt.ylim(-50,50)
plt.xlim(-50, 50)
plt.ylim(-50, 50)
# plt.show()
plt.plot(nodes[0], nodes[1], 'k-')
r = np.dot(Rot_mat, nodes)
#plt.plot(r[0], r[1], 'r-')
plt.show()
print ac_nodes
print nodes
print(ac_nodes)
print(nodes)
bla
node = np.array([-float(splitted[2]),
......
'''
Performing reader for vertices of a ac3d file
Created on 06.04.2015
@author: keith.paterson
'''
from re import split
import numpy as np
'''
Fast dedicated vertice reader for ac3d files
'''
class File(object):
def __init__(self, file_name, stats):
self.vertices = []
if file_name != None:
self.read(file_name)
def read(self, file_name):
with open(file_name) as f:
lines = f.readlines()
num_vertices = 0
for line in lines:
if line.startswith("numvert"):
num_vertices = int(line[8:])
continue
if num_vertices>0:
num_vertices-=1
s = split("[ \t]",line)
vertice = map(float,split("[ \t]",line))
self.vertices.append(vertice)
def nodes_as_array(self):
return np.array(self.vertices)
'''
Performing reader for vertices of a ac3d file
Created on 06.04.2015
@author: keith.paterson
'''
from re import split
import numpy as np
'''
Fast dedicated vertice reader for ac3d files
'''
class File(object):
def __init__(self, file_name, stats):
self.vertices = []
if file_name is not None:
self.read(file_name)
def read(self, file_name):
with open(file_name) as f:
lines = f.readlines()
num_vertices = 0
for line in lines:
if line.startswith("numvert"):
num_vertices = int(line[8:])
continue
if num_vertices>0:
num_vertices-=1
s = split("[ \t]", line)
vertice = list(map(float, split("[ \t]", line)))
self.vertices.append(vertice)
def nodes_as_array(self):
return np.array(self.vertices)
This diff is collapsed.
"""
Created on 27.04.2014
@author: keith.paterson
"""
import re
import telnetlib
import time
class FG_Telnet(object):
def __init__(self, host, port):
self.host = host
self.sock = telnetlib.Telnet(host, port)
self.sock.write("data")
def get_prop(self, property_name):
self.sock.write("get " + property_name + "\r\n")
result = self.sock.read_until("/>", 60)
return result
def set_prop(self, property_name, value):
send_buffer = "set %s %s\r\n" % (property_name, value)
self.sock.write(send_buffer)
result = self.sock.read_until("/>", 60)
return result
def run_command(self, command):
try:
send_buffer = "run %s\r\n" % command
self.sock.write(send_buffer)
result = self.sock.read_until("/>", 12000)
print result
if result.find("<completed>") >= 0:
return True
return False
except Exception, e:
print e
return False
def get_elevation(self, lon, lat):
self.set_prop("/position/latitude-deg", lat)
self.set_prop("/position/longitude-deg", lon)
self.set_prop("/position/altitude-ft", 5000)
time.sleep(0.05)
prop = self.get_prop("/position/ground-elev-m")
if prop is None:
return 0
elev = re.search('[^\']*\'([-e0-9.]*)\'[.]*', prop).group(1)
return float(elev)
def close(self):
self.sock.close()
if __name__ == '__main__':
fg = FG_Telnet("localhost", 5501)
fg.set_prop("/position/longitude-deg", -5)
fg.set_prop("/position/altitude-ft", 5000)
fg.get_prop("/position/longitude-deg")
time.sleep(1)
fg.get_prop("/position/longitude-deg")
time.sleep(1)
fg.set_prop("/position/latitude-deg", 56.4)
fg.get_prop("/position/ground-elev-m")
fg.set_prop("/position/longitude-deg", -6)
fg.get_prop("/position/longitude-deg")
time.sleep(1)
fg.get_prop("/position/longitude-deg")
time.sleep(1)
fg.set_prop("/position/latitude-deg", 56)
fg.get_prop("/position/ground-elev-m")
"""
Created on 27.04.2014
@author: keith.paterson
"""
import re
import telnetlib
import time
class FG_Telnet(object):
PROMPT = bytes("/>", encoding="ascii")
def __init__(self, host, port):
self.host = host
self.sock = telnetlib.Telnet(host, port)
self.sock.write(bytes("data", encoding="ascii"))
def get_prop(self, property_name):
self.sock.write(bytes("get " + property_name + "\n", encoding="ascii"))
result = self.sock.read_until(self.PROMPT, 60)
return result
def set_prop(self, property_name, value):
send_buffer = bytes("set %s %s\n" % (property_name, value), encoding="ascii")
self.sock.write(send_buffer)
result = self.sock.read_until(self.PROMPT, 60)
return result
def run_command(self, command):
try:
send_buffer = bytes("run %s\n" % command, encoding="ascii")
self.sock.write(send_buffer)
result = self.sock.read_until(self.PROMPT, 12000)
result_str = result.decode("ascii")
print(result)
if result_str.find("<completed>") >= 0:
return True
return False
except Exception as e:
print(e)
return False
def get_elevation(self, lon, lat):
self.set_prop("/position/latitude-deg", lat)
self.set_prop("/position/longitude-deg", lon)
self.set_prop("/position/altitude-ft", 5000)
time.sleep(0.05)
prop = self.get_prop("/position/ground-elev-m")
if prop is None:
return 0
elev = re.search('[^\']*\'([-e0-9.]*)\'[.]*', prop).group(1)
return float(elev)
def close(self):
self.sock.close()
if __name__ == '__main__':
fg = FG_Telnet("localhost", 5501)
fg.set_prop("/position/longitude-deg", -5)
fg.set_prop("/position/altitude-ft", 5000)
fg.get_prop("/position/longitude-deg")
time.sleep(1)
fg.get_prop("/position/longitude-deg")
time.sleep(1)
fg.set_prop("/position/latitude-deg", 56.4)
fg.get_prop("/position/ground-elev-m")
fg.set_prop("/position/longitude-deg", -6)
fg.get_prop("/position/longitude-deg")
time.sleep(1)
fg.get_prop("/position/longitude-deg")
time.sleep(1)
fg.set_prop("/position/latitude-deg", 56)
fg.get_prop("/position/ground-elev-m")
......@@ -101,7 +101,6 @@ def _get_nodes_from_acs(objs, own_prefix):
for b in objs:
fname = b.name
# print "in objs <%s>" % b.name
if fname.endswith(".xml"):
if fname.startswith(own_prefix):
continue
......@@ -136,10 +135,10 @@ def _get_nodes_from_acs(objs, own_prefix):
transposed_ac_nodes = -np.delete(ac.nodes_as_array().transpose(), 1, 0)[::-1]
transposed_ac_nodes = np.dot(Rot_mat, transposed_ac_nodes)
transposed_ac_nodes += b.anchor.as_array().reshape(2,1)
transposed_ac_nodes += b.anchor.as_array().reshape(2, 1)
all_nodes = np.append(all_nodes, transposed_ac_nodes.transpose(), 0)
except Exception, e:
logging.error("Error reading %s %s" % (fname,e))
except Exception as e:
logging.error("Error reading %s %s" % (fname, e))
return all_nodes
......@@ -204,7 +203,7 @@ def _compute_height_and_levels(b):
"""Determines total height (and number of levels) of a building based on
OSM values and other logic"""
try:
if isinstance(b.height, (int, long)):
if isinstance(b.height, (int)):
b.height = float(b.height)
assert(isinstance(b.height, float))
except AssertionError:
......@@ -293,7 +292,7 @@ def _compute_roof_height(b, max_height=1e99):
X = b.X
p0 = (X[0][0], X[0][1])
for i in range(0,len(X)):
for i in range(0, len(X)):
# compute coord in new referentiel
vecA = (X[i][0]-p0[0], X[i][1]-p0[1])
X2.append(vecA)
......@@ -426,7 +425,7 @@ def analyse(buildings, static_objects, transform, elev, facades, roofs):
try:
tools.stats.nodes_simplified += b.simplify(parameters.BUILDING_SIMPLIFY_TOLERANCE)
b.roll_inner_nodes()
except Exception, reason:
except Exception as reason:
logging.warn("simplify or roll_inner_nodes failed (OSM ID %i, %s)", b.osm_id, reason)
continue
......@@ -552,13 +551,13 @@ def analyse(buildings, static_objects, transform, elev, facades, roofs):
facade_requires.append('compat:roof-flat')
try:
if 'terminal' in string.lower(b.tags['aeroway']):
if 'terminal' in b.tags['aeroway'].lower():
facade_requires.append('facade:shape:terminal')
except KeyError:
pass
try:
if 'building:material' not in b.tags :
if b.tags['building:part'] == "column" :
if 'building:material' not in b.tags:
if b.tags['building:part'] == "column":
facade_requires.append(str('facade:building:material:stone'))
except KeyError:
pass
......@@ -570,11 +569,11 @@ def analyse(buildings, static_objects, transform, elev, facades, roofs):
del(b.tags['building:color'])
elif 'building:color' in b.tags and 'building:colour' in b.tags:
del(b.tags['building:color'])
facade_requires.append('facade:building:colour:'+string.lower(b.tags['building:colour']))
facade_requires.append('facade:building:colour:' + b.tags['building:colour'].lower())
except KeyError:
pass
try:
material_type = string.lower(b.tags['building:material'])
material_type = b.tags['building:material'].lower()
if str(material_type) in ['stone', 'brick', 'timber_framing', 'concrete', 'glass']:
facade_requires.append(str('facade:building:material:' + str(material_type)))
......@@ -894,7 +893,7 @@ def _write_ground(out, b, elev): # not used anywhere
angle = math.atan2(Xo[i1, 1] - Xo[i, 1], Xo[i1, 0] - Xo[i, 0])
l = ((Xo[i1, 1] - Xo[i, 1]) ** 2 + (Xo[i1, 0] - Xo[i, 0]) ** 2) ** 0.5
print l, b.lenX[i]
print(l, b.lenX[i])
# assert (l == b.lenX[i])
# angle = 10./57.3
R = np.array([[cos(angle), sin(angle)],
......@@ -978,8 +977,8 @@ def _write_ring(out, b, ring, v0, texture, tex_y0, tex_y1):
j = i + b.first_node
jpp = ipp + b.first_node
out.face([ (j , tex_x0, tex_y0),
(jpp , tex_x1, tex_y0),
out.face([ (j, tex_x0, tex_y0),
(jpp, tex_x1, tex_y0),
(jpp + b._nnodes_ground, tex_x1, tex_y11),
(j + b._nnodes_ground, tex_x0, tex_y12) ],
swap_uv=texture.v_can_repeat)
......
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
shamelessly translated from calc-tile.pl
......@@ -44,15 +43,17 @@ def format_lat(lat):
return "n%02d" % int(lat)
def root_directory_name((lon, lat)):
def root_directory_name(xxx_todo_changeme):
"""Generate the directory name for a location."""
(lon, lat) = xxx_todo_changeme
lon_chunk = floor(lon/10.0) * 10
lat_chunk = floor(lat/10.0) * 10
return format_lon(lon_chunk) + format_lat(lat_chunk) + os.sep
def directory_name((lon, lat)):
def directory_name(xxx_todo_changeme1):
"""Generate the directory name for a location."""
(lon, lat) = xxx_todo_changeme1
lon_floor = floor(lon)
lat_floor = floor(lat)
lon_chunk = floor(lon/10.0) * 10
......@@ -61,7 +62,8 @@ def directory_name((lon, lat)):
+ format_lon(lon_floor) + format_lat(lat_floor)
def tile_index((lon, lat), x=0, y=0):
def tile_index(xxx_todo_changeme2, x=0, y=0):
(lon, lat) = xxx_todo_changeme2
if x == 0 and y == 0:
y = calc_y(lat)
x = calc_x(lon, lat)
......@@ -153,5 +155,5 @@ if __name__ == "__main__":
(13.775, 51.9638889, 3171195),
(0.258094, 29.226081, 2956745),
(-2.216667, 30.008333, 2907651)):
print tile_index([lon, lat]) - idx
print directory_name([lon, lat])
print(tile_index([lon, lat]) - idx)
print(directory_name([lon, lat]))
'''
Created on 25.05.2015
@author: keith.paterson
'''
import os
import argparse
import logging
import stg_io2
import re
def scan_dir(dir):
try:
if os.path.isfile(dir) or "/" in dir:
return
files = os.listdir(dir)
refs = {}
#First scan the stgs
for file in files:
if file.endswith(".stg"):
stg = stg_io2.read_stg_entries(dir + os.sep + file, "", ignore_bad_lines=True)
for entry in stg:
if not "/" in entry.obj_filename and not "\\" in entry.obj_filename:
refs[entry.obj_filename] = dir
# print entry.obj_filename
#Then the xmls
for file in files:
if file.endswith(".xml") and file in refs:
with open(dir + os.sep + file) as f:
content = f.readlines()
for line in content:
if "<path>" in line:
fname = re.split("</?path>",line)[1]
refs[fname] = dir
break
f.close()
for file in files:
if file.endswith(".stg") or file.endswith(".eff"):
continue
elif os.path.isfile(dir + os.sep + file):
if not file in refs:
print dir + os.sep + file
os.remove(dir + os.sep + file)
for file in files:
if not os.path.isfile(dir + os.sep + file) and not ".svn" in file:
scan_dir(dir + os.sep + file)
# print file
except WindowsError, e:
# print e
pass
if __name__ == "__main__":
logging.basicConfig()
parser = argparse.ArgumentParser(description="clean unreferenced files scans the directory and deletes files not referenced in .stg(s)")
parser.add_argument("-d", dest="dir", help="the directory to be scanned")
args = parser.parse_args()
print args.dir
scan_dir(args.dir)
'''
Created on 25.05.2015
@author: keith.paterson
'''
import os
import argparse
import logging
import stg_io2