Created
May 30, 2019 19:11
-
-
Save jccartwright/8e43c353b6c4a508c91603cb61ca88ae to your computer and use it in GitHub Desktop.
read a gzipped GeoJSON file and write a CSV
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 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