def region2features(region):
  features = []
  print region
  # 1_4741
  m = re.search('/([\d_r]+).kmz',region)
  kml_name = m.group(1) + ".kml"
  with ZipFile(region,'r') as myzip:
    root = etree.parse(myzip.open(kml_name))
    for placemark in xpath(root,"n:Document/n:Placemark"):
      name = xpatht(placemark,"n:name")
      lon = xpathf(placemark,"n:Model/n:Location/n:longitude")
      lat = xpathf(placemark,"n:Model/n:Location/n:latitude")
      link = xpatht(placemark,"n:Model/n:Link/n:href")
      # assert location transformation is identity
      xml = myzip.open(link)
      model = etree.parse(xml)
      repairDAE(model)
      c = Collada(StringIO.StringIO(etree.tostring(model)))

      tris = []
      height = None
      for geom in c.geometries:
        assert len(geom.primitives) == 1
        triset = geom.primitives[0]
        heights = np.unique(np.hstack([[point[2] for point in tri] for tri in triset.vertex[triset.vertex_index]]))
        if len(heights) == 1:
          height = float(heights[0]) * inch_to_meters
          # the mesh is parallel to the xy plane
          for tri in triset.vertex[triset.vertex_index]:
            tri_p = Polygon([(tri[0][0],tri[0][1]),(tri[1][0],tri[1][1]),(tri[2][0],tri[2][1])])
            tris.append(tri_p)

      def r(t):
        try:
          if t.area > 0:
            return True
        except:
          pass
        return False

      valid_tris = filter(r,tris)
      footprint = unary_union(valid_tris)
      assert height is not None

      # http://spatialreference.org/ref/epsg/3826/html/
      twd97 = Proj(init='epsg:3826')

      def unproject_twd97(x,y):
        x_meters = x * inch_to_meters
        y_meters = y * inch_to_meters
        # convert the model origin to TWD97
        x0,y0 = twd97(lon,lat)
        return twd97(x0 + x_meters,y0+y_meters,inverse=True)

      unprojected = transform(unproject_twd97,footprint)
      cur.execute("INSERT INTO buildings(id, geom, height) VALUES (%s,ST_SetSRID(%s::geometry,4326),%s)",(name,unprojected.wkt,height))
      conn.commit()
  return features
Beispiel #2
0
import os
import json
from util import xpath, xpatht, xpathf

from lxml import etree
from shapely.geometry import box, mapping

# Gather all directories from the root KML file.
root = etree.parse("kmzs/Taipei3DBuilding_nl.kml")

features = []

for folder in xpath(root,"/n:kml/n:Document/n:NetworkLink"):
  folder_name = xpatht(folder,"n:name")
  print "Traversing folder " + folder_name
  folder_link = xpatht(folder,"n:Link/n:href").replace("\\","/")
  folder_root = etree.parse("kmzs/" + folder_link)
  for superregion in xpath(folder_root,"/n:kml/n:Folder/n:NetworkLink"):
    sr_name = xpatht(superregion,"n:name")
    print "Traversing super region " + sr_name
    sr_link  = xpatht(superregion,"n:Link/n:href").replace("\\","/")
    sr_north = xpathf(superregion,"n:Region/n:LatLonAltBox/n:north")
    sr_south = xpathf(superregion,"n:Region/n:LatLonAltBox/n:south")
    sr_east  = xpathf(superregion,"n:Region/n:LatLonAltBox/n:east")
    sr_west  = xpathf(superregion,"n:Region/n:LatLonAltBox/n:west")
    #print north, south, east, west, link, name
    sr_path = "kmzs/" + folder_name + "/" + sr_link
    sr_root = etree.parse(sr_path)
    for region in xpath(sr_root,"/n:kml/n:Document/n:NetworkLink"):
      # altitude?
      r_name = xpatht(region,"n:name")