Commit 13c8be31 authored by Thomas Albrecht's avatar Thomas Albrecht

stats moved to tools

parent d9dfb001
......@@ -20,51 +20,19 @@ import sys
import textwrap
import plot
class Stats(object):
def __init__(self):
self.objects = 0
self.skipped = 0
self.buildings_in_LOD = np.zeros(3)
self.area_levels = np.array([1,10,20,50,100,200,500,1000,2000,5000,10000,20000,50000])
self.area_above = np.zeros_like(self.area_levels)
self.vertices = 0
self.surfaces = 0
self.out = open("small.dat", "w")
def count(self, area):
for i in range(len(self.area_levels))[::-1]:
if area >= self.area_levels[i]:
self.area_above[i] += 1
return i
self.area_above[0] += 1
return 0
def print_summary(self):
out = sys.stdout
out.write(textwrap.dedent("""
total buildings %i
skipped %i
vertices %i
surfaces %i
""" % (self.objects, self.skipped, self.vertices, self.surfaces)))
for i in range(len(self.area_levels)):
out.write("> %5g m^2 %5i\n" % (self.area_levels[i], self.area_above[i]))
stats = Stats()
import tools
def write_and_count_numvert(out, numvert):
"""write numvert tag to .ac, update stats"""
out.write("numvert %i\n" % numvert)
global stats
stats.vertices += numvert
#global stats
tools.stats.vertices += numvert
def write_and_count_numsurf(out, numsurf):
"""write numsurf tag to .ac, update stats"""
out.write("numsurf %i\n" % numsurf)
global stats
stats.surfaces += numsurf
#global stats
tools.stats.surfaces += numsurf
class random_number(object):
def __init__(self, randtype, min, max):
......@@ -119,8 +87,11 @@ def reset_nb():
global nb
nb = 0
def analyse(buildings, static_objects):
"""analyse all buildings"""
pass
def write_building(b, out, elev, tile_elev, transform, offset, facades, roofs, LOD_lists):
def write(b, out, elev, tile_elev, transform, offset, facades, roofs, LOD_lists):
"""offset accounts for cluster center"""
# am anfang geometrieanalyse
# - ort: urban, residential, rural
......@@ -132,8 +103,9 @@ def write_building(b, out, elev, tile_elev, transform, offset, facades, roofs, L
# - facade raussuchen
# requires: compat:flat-roof
global first
# global first
global stats
#mat = random.randint(1,4)
mat = 0
roof_mat = 0
......@@ -240,8 +212,9 @@ def write_building(b, out, elev, tile_elev, transform, offset, facades, roofs, L
# stats.out.write("\n")
# stats.out.write('# '+str(list(r.coords)))
# stats.out.write("\n\n")
stats.count(p.area)
print "Stats obj", tools.stats
tools.stats.count(p.area)
tools.stats.print_summary()
# if p.area < 200.:
# print "small?", p.area
#
......
......@@ -41,6 +41,7 @@ You should disable random buildings.
# - decide LOD
# - write clusters
import pdb
import numpy as np
import sys
......@@ -51,14 +52,13 @@ from imposm.parser import OSMParser
import coordinates
import itertools
from cluster import Clusters
from building_writer import write_building, random_number
from building_writer import stats
import building_lib
#from building_writer import write_building, random_number
from vec2d import vec2d
import textwrap
import pdb
import textures as tex
import stg
import stg_io
import tools
tex.init()
......@@ -67,10 +67,8 @@ print tex.roofs
# -- defaults
ground_height = -20
nb = 0
nobjects = 0
buildings = [] # -- master list, holds all buildings
from building_writer import stats
first = True
tile_size_x=500 # -- our tile size in meters
......@@ -90,8 +88,6 @@ skiplist = ["Dresden Hauptbahnhof", "Semperoper", "Zwinger", "Hofkirche",
def dist(a,b):
pass
class Building(object):
"""Central object class.
Holds all data relevant for a building. Coordinates, type, area, ..."""
......@@ -105,7 +101,8 @@ class Building(object):
self.area = 0
global transform
r = self.refs[0]
self.X = vec2d(transform.toLocal((r.lat, r.lon)))
self.anchor = vec2d(transform.toLocal((r.lat, r.lon)))
#print "tr X", self.X
class Coords(object):
......@@ -164,9 +161,9 @@ class wayExtract(object):
self.buildings.append(building)
# global stats
if stats.objects == total_objects: raise ValueError
stats.objects += 1
print stats.objects
if tools.stats.objects == total_objects: raise ValueError
tools.stats.objects += 1
print tools.stats.objects
#global clusters
#clusters.append(building.X, building)
......@@ -234,10 +231,6 @@ if 0:
#sys.exit(0)
elev = tools.Interpolator("elev.xml", fake=False) # -- fake skips actually reading the file, speeding up things
print "height at origin", elev(vec2d(0,0))
print "origin at ", transform.toGlobal((0,0))
def write_map(filename, transform, elev, gmin, gmax):
lmin = vec2d(transform.toLocal((gmin.x, gmin.y)))
......@@ -403,89 +396,101 @@ def write_xml(fname, LOD_lists):
# here we go!
# -----------------------------------------------------------------------------
# - parse OSM -> return a list of building objects
if __name__ == "__main__":
#print clusters.list
# instantiate counter and parser and start parsing
way = wayExtract()
#p = OSMParser(concurrency=4, ways_callback=way.ways, coords_callback=way.coords )
p = OSMParser(concurrency=4, coords_callback=way.coords )
print "start parsing coords"
p.parse(infile)
print "done parsing"
print "ncords:", len(way.coords_list)
print "bounds:", way.minlon, way.maxlon, way.minlat, way.maxlat
p = OSMParser(concurrency=4, ways_callback=way.ways)
print "start parsing ways"
try:
p.parse(infile)
except ValueError:
pass
# from tools import stats
# global stats
print ">> stats", tools.stats
tools.init()
print ">> stats", tools.stats
tools.stats.print_summary()
print "nbuildings", len(way.buildings)
print "done parsing"
buildings = way.buildings
elev = tools.Interpolator("elev.xml", fake=False) # -- fake skips actually reading the file, speeding up things
print "height at origin", elev(vec2d(0,0))
print "origin at ", transform.toGlobal((0,0))
# - read relevant stgs
stg.read() # FIXME: dummy
# - analyze buildings
# - calculate area
# - location clash with stg static models? drop building
# - analyze surrounding: similar shaped buildings nearby? will get same texture
# - set building type, roof type etc
# - decide LOD
# b_lib.analyze(buildings)
# -- now put buildings into clusters
clusters = Clusters(min_, max_, vec2d(2000.,2000.))
for b in buildings:
clusters.append(b.X, b)
clusters.stats()
# - write clusters
stg = open("city.stg", "w")
for l in clusters._clusters:
for cl in l:
nb = len(cl.objects)
if not nb: continue
# -- get cluster center
offset = cl.center
tile_elev = elev(cl.center)
#transform.setOffset((-offset).list())
center_lat, center_lon = transform.toGlobal((cl.center.x, cl.center.y))
LOD_lists = []
LOD_lists.append([])
LOD_lists.append([])
LOD_lists.append([])
# - parse OSM -> return a list of building objects
#print clusters.list
# instantiate counter and parser and start parsing
way = wayExtract()
# -- open ac and write header
fname = "city-%04i%04i" % (cl.I.x, cl.I.y)
out = open(fname+".ac", "w")
write_ac_header(out, nb)
for building in cl.objects:
write_building(building, out, elev, tile_elev, transform, offset, tex.facades, tex.roofs, LOD_lists)
out.close()
#transform.setOffset((0,0))
# -- write xml
write_xml(fname, LOD_lists)
# -- write stg
stg.write("OBJECT_STATIC %s %g %g %g %g\n" % (fname+".xml", center_lon, center_lat, tile_elev, 180))
stg.close()
print "done writing ac's"
stats.print_summary()
stats.out.close()
sys.exit(0)
#p = OSMParser(concurrency=4, ways_callback=way.ways, coords_callback=way.coords )
p = OSMParser(concurrency=4, coords_callback=way.coords )
print "start parsing coords"
p.parse(infile)
print "done parsing"
print "ncords:", len(way.coords_list)
print "bounds:", way.minlon, way.maxlon, way.minlat, way.maxlat
p = OSMParser(concurrency=4, ways_callback=way.ways)
print "start parsing ways"
try:
p.parse(infile)
except ValueError:
pass
print "nbuildings", len(way.buildings)
print "done parsing"
buildings = way.buildings
# - read relevant stgs
static_objects = stg_io.Stg("e013n51/3171138.stg")
# - analyze buildings
# - calculate area
# - location clash with stg static models? drop building
# - analyze surrounding: similar shaped buildings nearby? will get same texture
# - set building type, roof type etc
# - decide LOD
building_lib.analyse(buildings, static_objects)
# -- now put buildings into clusters
clusters = Clusters(min_, max_, vec2d(2000.,2000.))
for b in buildings:
clusters.append(b.anchor, b)
clusters.stats()
# - write clusters
stg = open("city.stg", "w")
for l in clusters._clusters:
for cl in l:
nb = len(cl.objects)
if not nb: continue
# -- get cluster center
offset = cl.center
tile_elev = elev(cl.center)
#transform.setOffset((-offset).list())
center_lat, center_lon = transform.toGlobal((cl.center.x, cl.center.y))
LOD_lists = []
LOD_lists.append([])
LOD_lists.append([])
LOD_lists.append([])
# -- open ac and write header
fname = "city-%04i%04i" % (cl.I.x, cl.I.y)
out = open(fname+".ac", "w")
write_ac_header(out, nb)
for building in cl.objects:
building_lib.write(building, out, elev, tile_elev, transform, offset, tex.facades, tex.roofs, LOD_lists)
out.close()
#transform.setOffset((0,0))
# -- write xml
write_xml(fname, LOD_lists)
# -- write stg
stg.write("OBJECT_STATIC %s %g %g %g %g\n" % (fname+".xml", center_lon, center_lat, tile_elev, 180))
stg.close()
print "done writing ac's"
tools.stats.print_summary()
tools.stats.out.close()
sys.exit(0)
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 23 18:32:18 2013
@author: tom
"""
def read():
pass
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 23 18:32:18 2013
@author: tom
"""
#def read():
# return None
from osm2city import Building, Coords
inf="/usr/share/games/FlightGear/Scenery/Objects/w130n30/w122n37/958440.stg"
# def __str__(self):
# return "%s %s %g %g %g %g" % (self.typ, self.path, self.lon, self.lat, self.alt, self.hdg)
class Stg:
def __init__(self, filename):
self.objs = self.read(filename)
def read(self, filename):
objs = []
f = open(filename)
for line in f.readlines():
if line.startswith('#'): continue
splitted = line.split()
typ, path = splitted[0:2]
lon = float(splitted[2])
lat = float(splitted[3])
alt = float(splitted[4])
r = Coords(-1, lon, lat)
hdg = float(splitted[5])
objs.append(Building(osm_id=-1, tags=-1, refs=[r], name="", height=0, levels=0))
f.close()
return objs
#a = stg(inf)
#for o in a.objs:
# print o
......@@ -276,6 +276,7 @@ def init():
if __name__ == "__main__":
init()
bla
cands = facades.find_candidates([], 14)
print "cands ar", cands
for t in cands:
......
......@@ -8,6 +8,9 @@ Created on Sat Mar 23 18:42:23 2013
@author: tom
"""
import numpy as np
import sys
import textwrap
stats = None
class Interpolator(object):
"""load elevation data from file, interpolate"""
......@@ -64,3 +67,41 @@ def raster(transform, fname, x0, y0, size_x=1000, size_y=1000, step_x=5, step_y=
f.write("%1.8f %1.8f %g %g\n" % (lon, lat, x, y))
f.write("\n")
f.close()
class Stats(object):
def __init__(self):
self.objects = 0
self.skipped = 0
self.buildings_in_LOD = np.zeros(3)
self.area_levels = np.array([1,10,20,50,100,200,500,1000,2000,5000,10000,20000,50000])
self.area_above = np.zeros_like(self.area_levels)
self.vertices = 0
self.surfaces = 0
self.out = open("small.dat", "w")
def count(self, area):
print "COUTNING", area,
for i in range(len(self.area_levels))[::-1]:
if area >= self.area_levels[i]:
self.area_above[i] += 1
print self.area_above[i]
return i
self.area_above[0] += 1
return 0
def print_summary(self):
out = sys.stdout
out.write(textwrap.dedent("""
total buildings %i
skipped %i
vertices %i
surfaces %i
""" % (self.objects, self.skipped, self.vertices, self.surfaces)))
for i in range(len(self.area_levels)):
out.write(" %5g m^2 %5i\n" % (self.area_levels[i], self.area_above[i]))
print self
def init():
global stats
stats = Stats()
print "tools: init", stats
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment