Skip to content

Instantly share code, notes, and snippets.

@kareblak
Last active December 5, 2016 14:47
Show Gist options
  • Save kareblak/d3db3426a47493c0e872 to your computer and use it in GitHub Desktop.
Save kareblak/d3db3426a47493c0e872 to your computer and use it in GitHub Desktop.
Convert from MySQL area dump to PostGIS insert statements
import java.io.FileOutputStream
Console.withOut(new FileOutputStream("/Users/kareblak/Downloads/area.sql")) {
println(
scala.io.Source.fromFile("/Users/kareblak/Downloads/areas.csv").getLines.toList.tail.map(_.split(',')).groupBy {
case Array(_, n, _*) => n
}.map {
case (k, vs) => k -> vs.collect {
case Array(_, _, _, c) => c
}
}.map {
case (k, vs) => k -> vs.grouped(2).collect {
case Seq(a, b) => a -> b
}.toList
}.collect {
case (k, vs) if vs.nonEmpty =>
k -> (if (vs.head != vs.last) vs :+ vs.head else vs)
}.map {
case (k, vs) =>
val poly = vs.map {
case (lat, lon) => s"($lat,$lon)"
}.mkString("(",",",")").trim
s"""('${k.replace("\"","").replace("'","")}', '$poly'::polygon, 1)"""
}.mkString("INSERT INTO area (name, polygon, user_id) VALUES \n",",\n",";")
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment