Skip to content

Instantly share code, notes, and snippets.

@sabman
Created November 5, 2009 02:44
Show Gist options
  • Save sabman/226653 to your computer and use it in GitHub Desktop.
Save sabman/226653 to your computer and use it in GitHub Desktop.
gml_helper.rb
require 'rubygems'
require 'geo_ruby'
include GeoRuby::SimpleFeatures
module GmlHelper
def parse_gml(gml_str)
doc = Hpricot(gml_str)
coords = doc.search('gml:coordinates').inner_html.split(',')
unless coords.empty? or coords.nil?
if doc.search("gml:point").size == 1
#Hpricot('<gml:Point srsName="SDO:8311" xmlns:gml="http://www.opengis.net/gml"><gml:coordinates decimal="." cs="," ts=" ">111.733,-15.783, </gml:coordinates></gml:Point>')
point = Point.from_x_y(coords[0], coords[1])
point.as_wkt
elsif doc.search("gml:linestring").size == 1
#Hpricot('<gml:LineString srsName="SDO:8311" xmlns:gml="http://www.opengis.net/gml"><gml:coordinates decimal="." cs="," ts=" ">128.62768333,-33.64501667, 128.6114,-33.64481667, </gml:coordinates></gml:LineString>')
coords.delete_at(coords.size-1)
linecoords = []
(0..(coords.size/2)-1).each{ |i| linecoords << [coords[i*2].to_f, coords[i*2+1].to_f]}
linestring = LineString.from_coordinates(linecoords)
linestring.as_wkt
end
else
raise "Unsupported GML Geomtry"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment