Commit b89abe89 authored by duminil's avatar duminil
Browse files

Removing old scripts

parent c548ba99
module ConvexHull
# after graham & andrew
def self.calculate(points)
lop = points.sort
left = lop.shift
right = lop.pop
lower, upper = [left], [left]
lower_hull, upper_hull = [], []
det_func = determinant_function(left, right)
until lop.empty?
p = lop.shift
( det_func.call(p) < 0 ? lower : upper ) << p
end
lower << right
until lower.empty?
lower_hull << lower.shift
while (lower_hull.size >= 3) &&
!convex?(lower_hull.last(3), true)
last = lower_hull.pop
lower_hull.pop
lower_hull << last
end
end
upper << right
until upper.empty?
upper_hull << upper.shift
while (upper_hull.size >= 3) &&
!convex?(upper_hull.last(3), false)
last = upper_hull.pop
upper_hull.pop
upper_hull << last
end
end
upper_hull.shift
upper_hull.pop
lower_hull + upper_hull.reverse+[left]
end
private
def self.determinant_function(p0, p1)
proc { |p| ((p0.x-p1.x)*(p.y-p1.y))-((p.x-p1.x)*(p0.y-p1.y)) }
end
def self.convex?(list_of_three, lower)
p0, p1, p2 = list_of_three
(determinant_function(p0, p2).call(p1) > 0) ^ lower
end
end
# sudo aptitude install libproj-dev
# gem install proj4rb
# This Ruby script :
# finds all the citygml files
# reads them
# find the 2D convex hull
# writes it to a kml file
require 'fileutils'
require 'proj4'
require 'builder'
require_relative 'convex_hull'
Resolution = 0.1
RepositoryDir = "../TestRepository/"
class Point
attr_reader :x,:y,:z
@@dest_prj = Proj4::Projection.new("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
@@src_prj = Proj4::Projection.new("+proj=tmerc +lat_0=0 +lon_0=9 +k=1 +x_0=3500000 +y_0=0 +ellps=bessel +datum=potsdam +units=m +no_defs")
def initialize(l)
@x,@y,@z = l.map{|v| v.to_f}
end
def to_s
[x,y,z].join(',')
end
def to_wgs84
wgs84_point = @@src_prj.transform(@@dest_prj, proj4_point)
['%.7f' % (wgs84_point.x*180/Math::PI), '%.7f' % (wgs84_point.y*180/Math::PI),0].join(',')
end
def <=>(p2)
[x,y]<=>[p2.x,p2.y]
end
private
def proj4_point
Proj4::Point.new(x,y)
end
end
xml = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2)
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.kml do
xml.Document do
FileUtils.cd(RepositoryDir){
xmls_and_gmls = Dir["**/*"].select{|f|
File.extname(f).downcase=~/(g|x)ml/
}.sort_by{|f| File.basename(f)}
citygmls = xmls_and_gmls.select{|xml|
File.open(xml,'r'){|f|
f.read(1000)
}=~/citymodel/i
}
citygmls.each{|citygml|
basename = File.basename(citygml)
project = File.dirname(citygml).split('/').last.gsub('.simstadt','')
content = File.read(citygml).force_encoding("ISO-8859-1").encode("utf-8", replace: nil)
coordinates = content.gsub(/(low|upp)erCorner.*?Corner/,'').scan(/(?<![\d\.])(3\d\d\d\d\d\d[\.\d]*) (5\d\d\d\d\d\d[\.\d]*) ([\d\.]+)/)
next if coordinates.empty?
xml.Placemark do
xml.name basename
xml.description project+">"+basename
xml.Polygon do
xml.outerBoundaryIs do
xml.LinearRing do
xml.tessellate 1
xyz_points = coordinates.map{|l| Point.new(l)}
xy_points = xyz_points.uniq{|p| [(p.x/Resolution).round,(p.y/Resolution).round]}
hull = ConvexHull.calculate(xy_points).map{|p| p.to_wgs84}
xml.coordinates hull.join(' ')
end
end
end
end
}
}
end
end
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