Skip to content

Instantly share code, notes, and snippets.

@sabman
Created November 25, 2009 08:11
Show Gist options
  • Save sabman/242559 to your computer and use it in GitHub Desktop.
Save sabman/242559 to your computer and use it in GitHub Desktop.
mars_helper.rb # ausome helper for my MARS :-)
module MarsHelper
def self.convert_params_spatial_values_to_georuby(h)
geom = nil
r = h
if h[:start_lat] and h[:end_lat] and h[:start_long] and h[:end_long] # linestring
if r[:start_depth]
geom = LineString.from_coordinates([[r[:start_long], r[:start_lat],r[:start_depth].abs*-1],[r[:end_long], r[:end_lat], r[:end_depth].abs*-1]], 8311, true)
else
geom = LineString.from_coordinates([[r[:start_long], r[:start_lat]], [r[:end_long], r[:end_lat]]], 8311)
end
elsif h[:start_lat] and (h[:end_lat].nil? or h[:end_long].nil?) # point
if r[:start_depth]
geom = Point.from_coordinates([r[:start_long], r[:start_lat], r[:start_depth].abs*-1], 8311, true)
else
geom = Point.from_coordinates([r[:start_long], r[:start_lat]], 8311)
end
end
r.delete(:start_long); r.delete(:start_lat); r.delete(:end_long); r.delete(:end_lat); r.delete(:start_depth); r.delete(:end_depth)
r[:geom_original] = geom
r
end
def self.build_water_depth_params(opts = {})
params = {}
params.merge!(opts)
raise "quant_value must be defined as the water depth" if params[:quant_value].nil?
raise "sampleno must be defined" if params[:sampleno].nil?
params[:quant_value] = params[:quant_value] * -1 if params[:quant_value] > 0
params[:property] ||= "water depth"
params[:uom] ||= "m"
params[:squence_no] ||= 1
params[:ano] ||= 84
end
def self.create_water_depth_in_data(sample)
geom = sample.geom_original
raise "sample #{sample.sampleid} has no spatial info" if geom.nil?
raise "sample geometry has to be a georuby SimpleFeature type" unless geom.kind_of?(GeoRuby::SimpleFeatures::Geometry)
geom.points.each_with_index do |p, i|
params = {}
params[:sampleno] = sample.sampleno
params[:quant_value] = p.z
params[:sequence_no] = i+1
pp params
params = build_water_depth_params(params)
pp params
end
end
end
require 'rubygems'
require 'oci8'
OCI8::Object::Base.class_eval do
# module Mdsys; def SdoGeometry
def as_georuby
raise "geom should be a OCI8::Object::Mdsys::SdoGeometry" unless self.kind_of?(OCI8::Object::Mdsys::SdoGeometry)
attributes = self.instance_variable_get("@attributes")
# point?
gtype = attributes[:sdo_gtype].to_i
srid = attributes[:sdo_srid].to_i
# gtype = dl0t (d=dim, l=?, t=type)
m = gtype.to_s.match(/(\d)00(\d)/)
d = m.values_at(1)[0].to_i
t = m.values_at(2)[0].to_i
if t == 1 # point
p = attributes[:sdo_point].instance_variable_get("@attributes")
x = p[:x].to_f
y = p[:y].to_f
if d == 3 # 3D point
z = p[:z].to_f
georb = GeoRuby::SimpleFeatures::Point.from_coordinates([x,y,z], srid, true)
elsif d == 2 # 2D point
georb = GeoRuby::SimpleFeatures::Point.from_coordinates([x,y], srid)
else
raise "Cannot handle this dimension of point #{d}"
end
return georb
# s = Prod::Sample.find(1984851) # 2001
# s = Prod::Sample.find(1978952) # 3001
# #<OCI8::Object::Mdsys::SdoGeometry:0x3ed05a0
# @attributes=
# {:sdo_point=>
# #<OCI8::Object::Mdsys::SdoPointType:0x3ed01e0
# @attributes=
# {:x=>#<OraNumber:113.71801>, :y=>#<OraNumber:-30.01861>, :z=>nil},
# @con=nil>,
# :sdo_elem_info=>nil,
# :sdo_gtype=>#<OraNumber:3001>,
# :sdo_srid=>#<OraNumber:8311>,
# :sdo_ordinates=>nil},
# @con=nil>
elsif t==2 # LineString
coords = []
attributes[:sdo_ordinates].instance_variable_get("@attributes").each{|c| coords << c.to_f}
s = 0
e = s+d-1
coords_dim
while e < coords.size
coords_dim << coords[s..e]
s=e+1
e=s+d-1
end
if d == 3 # 3D line
georb = GeoRuby::SimpleFeatures::LineString.from_coordinates(coords_dim, srid, true)
elsif d == 2 # 2D line
georb = GeoRuby::SimpleFeatures::LineString.from_coordinates(coords_dim, srid)
else
raise "unsupported dimenstion for linestring: #{d}"
end
return georb
# s = Prod::Sample.find(2002718)
##<OCI8::Object::Mdsys::SdoGeometry:0x3fac5b4
# @attributes=
# {:sdo_point=>nil,
# :sdo_elem_info=>
# #<OCI8::Object::Mdsys::SdoElemInfoArray:0x3fab204
# @attributes=[#<OraNumber:1>, #<OraNumber:2>, #<OraNumber:1>],
# @con=nil>,
# :sdo_gtype=>#<OraNumber:3002>,
# :sdo_srid=>#<OraNumber:8311>,
# :sdo_ordinates=>
# #<OCI8::Object::Mdsys::SdoOrdinateArray:0x3faadcc
# @attributes=
# [#<OraNumber:110.85005>,
# #<OraNumber:-26.2319>,
# #<OraNumber:3107>,
# #<OraNumber:110.8473>,
# #<OraNumber:-26.23572>,
# #<OraNumber:3251>],
# @con=nil>},
# @con=nil>
else
raise "unidentified SDO_GEOMETRY type"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment