Skip to content

Instantly share code, notes, and snippets.

@tokumine
Created May 24, 2010 14:17
Show Gist options
  • Save tokumine/411915 to your computer and use it in GitHub Desktop.
Save tokumine/411915 to your computer and use it in GitHub Desktop.
module GeomHelper
def self.geojson_to_wkt geojson
#PARSE GEOJSON INTO NICE RING STRINGS
json = JSON.parse geojson
polys = json["coordinates"]
polygons = []
polys.each do |poly|
rings = []
poly.each do |ring|
r = ring.inject([]){|acc,x| acc << x.join(" ")}.join(",")
rings << "(#{r})"
end
polygons << rings
end
#WRITE OUT TO WKT
wkt = polygons.inject([]){|acc,x| acc << "(#{x.join(",")})"}
"MULTIPOLYGON(#{wkt.join(",")})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment