Skip to content

Instantly share code, notes, and snippets.

@jccartwright
Created May 30, 2019 19:11
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 jccartwright/8e43c353b6c4a508c91603cb61ca88ae to your computer and use it in GitHub Desktop.
Save jccartwright/8e43c353b6c4a508c91603cb61ca88ae to your computer and use it in GitHub Desktop.
read a gzipped GeoJSON file and write a CSV
import groovy.json.*
import groovy.io.FileType
import groovy.util.*
import java.util.zip.GZIPInputStream
def dataDir = new File("/data/csb_data")
assert dataDir.exists() && dataDir.isDirectory()
def outputDir = new File("/data/csb_data/csv")
assert outputDir.exists() && outputDir.isDirectory()
JsonSlurper jsonSlurper = new JsonSlurper()
new FileNameFinder().getFileNames(dataDir.path, "**/*.json.gz").each { zipFile ->
println "processing file ${zipFile}..."
//open output file
def basename = zipFile.split('\\/')[-1]
def outputFileName = basename.replace('_geoJson.json.gz','') + '.csv'
def outputFile = new File("${outputDir}/${outputFileName}")
if (outputFile.delete()) {
println "WARNING: deleting existing output file: ${outputFile}..."
}
//parse JSON and pull relevant attributes
GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(zipFile))
def json = jsonSlurper.parse(gzis)
//common attributes to all features
def platformName = json.properties.platform.name
def platformUniqueID = json.properties.platform.uniqueID
def orgName = ""
if (json.properties.providerContactPoint) {
orgName = json.properties.providerContactPoint.orgName
}
//write out each feature to output file
json.features.each { feature ->
//def attributes = [ feature.geometry.coordinates[0], feature.geometry.coordinates[1], feature.properties.depth, "\"${feature.properties.time}\"", "\"${orgName}\"", "\"${platformName}\"", "\"${platformUniqueID}\"" ]
//outputFile << "${attributes.join(',')}\n"
outputFile << """${feature.geometry.coordinates[0]},${feature.geometry.coordinates[1]},${feature.properties.depth},"${feature.properties.time}","${orgName}","${platformName}","${platformUniqueID}"\n"""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment