Last active
November 25, 2017 20:27
-
-
Save Brideau/ea1c98c089208fa363e12b2f8ee55154 to your computer and use it in GitHub Desktop.
Using ogr2ogr from Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If you need to add additional parameters, as is the case if creating a | |
// CSV file, you can use the -lco flag multiple times | |
val outPathCsv = Paths.get("temp", "Canada3573Csv", "Canada3573.csv").toAbsolutePath | |
val cmdCsv = Array("-f", "CSV", outPathCsv.toString, inPath.toString, | |
"-lco", "GEOMETRY=AS_WKT", | |
"-lco", "CREATE_CSVT=YES", | |
"-lco", "SEPARATOR=SEMICOLON" | |
) | |
convertUsingOgr2Ogr(outPathCsv, cmdCsv).onComplete{ | |
case Success(outPath) => println("Shapefile conversion successful. File is located at " + outPath) | |
case Failure(ex) => println("Shapefile conversion failed with error: " + ex.getMessage) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resolvers += "Boundless" at "http://repo.boundlessgeo.com/main/" | |
libraryDependencies += "org.gdal" % "gdal" % "2.2.1" | |
// These let you enter input in the terminal if running via SBT | |
fork in run := true | |
connectInput in run := true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Build the paths to the input and output files. You could also do this as | |
// Paths.get("/some/path/to/a/file.gpkg") if you don't care about this working | |
// across operating systems | |
val inPath = Paths.get("src", "main", "resources", "Canada3573.gpkg").toAbsolutePath | |
val outPathShp = Paths.get("temp", "Canada3573Shp", "Canada3573.shp").toAbsolutePath | |
// Build your command, putting each part of the command in a separate string | |
// stored as elements in an array | |
val cmdShp = Array("-f", "ESRI Shapefile", outPathShp.toString, inPath.toString) | |
// Execute the conversion asynchronously and print a success/failure message | |
convertUsingOgr2Ogr(outPathShp, cmdShp).onComplete{ | |
case Success(outPath) => println("Shapefile conversion successful. File is located at " + outPath) | |
case Failure(ex) => println("Shapefile conversion failed with error: " + ex.getMessage) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convertUsingOgr2Ogr(outPath: Path, cmd: Array[String]): Future[Path] = Future { | |
println("Starting conversion using ogr2ogr") | |
// Create the path to hold the file if it doesn't exist. Handy for keeping | |
// Shapefiles organized in their own sub-folders. | |
Files.createDirectories(outPath.toAbsolutePath.getParent) | |
ogr2ogr.execute(cmd) | |
// Return the path of the newly created file, out of convenience | |
outPath | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file._ | |
import org.gdal.apps.ogr2ogr | |
import scala.io.StdIn | |
import scala.util.{Failure, Success} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// More code above... | |
public static void execute(String[] args) | |
{ | |
String pszFormat = "ESRI Shapefile"; | |
String pszDataSource = null; | |
String pszDestDataSource = null; | |
Vector papszLayers = new Vector(); | |
// More code below... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment