Skip to content

Instantly share code, notes, and snippets.

@cbachich
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbachich/2422a1f02a4ffc4dcb23 to your computer and use it in GitHub Desktop.
Save cbachich/2422a1f02a4ffc4dcb23 to your computer and use it in GitHub Desktop.
Converting an array to a Multipolygon String for RGeo
test = [[[-70, 30],[-71, 31]],[[0,0],[-180,90],[-179,75]]]=> [[[-70, 30], [-71, 31]], [[0, 0], [-180, 90], [-179, 75]]]
converted = "MULTIPOLYGON ((#{test.map { |coord| "(#{coord.map { |pair| pair.join(" ") }.join ", "})" }.join ", "}))"
MULTIPOLYGON (((-70 30, -71 31), (0 0, -180 90, -179 75)))
@kyletolle
Copy link

Instead of having one line do everything (all the maps, joins, etc), use multiple lines. You could likely factor out methods at that point, which would increase readability for one thing, and make it easier to understand what's going on.

Still seems like performance is going to be an issue when you're traversing so many nested arrays, but I'm not immediately sure of a way around that.

multipolygon_string = "MULTIPOLYGON ((#{coordinates_string}))"

@cbachich
Copy link
Author

Yeah that is probably best. I'm still trying to figure out if there is a way to optimize this. Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment