Skip to content

Instantly share code, notes, and snippets.

@timwaters
Created October 11, 2011 19:58
Show Gist options
  • Save timwaters/1279220 to your computer and use it in GitHub Desktop.
Save timwaters/1279220 to your computer and use it in GitHub Desktop.
patch for old dbs geocommons geocoder
# Unpack an array of little-endian 4-byte ints, and convert them into
# signed floats by dividing by 10^6, inverting the process used by the
# compress_wkb_line() function in the SQLite helper extension.
def unpack_geometry (geom)
points = []
if !geom.nil?
# Pete - The database format is completely different to the one
# expected by the code, so I've done some detective work to
# figure out what it should be. It looks like the format is
# | 1 byte Type | 4 byte SRID | 4 byte element count| 8 byte double coordinates *
# I've added new code to read this, and commented out the old.
#info = geom.unpack('CVVD*')
# coords = info.slice(3, info.length)
#points << [coords.shift, coords.shift] until coords.empty?
coords = geom.unpack "V*" # little-endian 4-byte long ints
#
## now map them into signed floats
coords.map! {|i| ( i > (1 << 31) ? i - (1 << 32) : i ) / 1_000_000.0}
points << [coords.shift, coords.shift] until coords.empty?
end
points
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment