Skip to content

Commit

Permalink
Use orginfo json formatted output
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotjordan committed Sep 6, 2023
1 parent fa6f26b commit e8fb0dd
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 70 deletions.
35 changes: 20 additions & 15 deletions app/derivative_services/geo_derivatives/processors/vector/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ module GeoDerivatives
module Processors
module Vector
class Info
attr_accessor :doc
attr_accessor :path
attr_writer :name, :driver

def initialize(path)
@doc = ogrinfo(path)
@path = path
end

# Parsed json out from ogrinfo
# @return [Hash]
def doc
@doc ||= begin
JSON.parse(ogrinfo(path))
rescue
{}
end
end

# Returns the vector dataset name
Expand Down Expand Up @@ -40,46 +50,41 @@ def bounds
# @param path [String] path to vector file or shapefile directory
# @return [String] output of ogrinfo
def ogrinfo(path)
stdout, _stderr, _status = Open3.capture3("ogrinfo", "-ro", "-so", "-al", path.to_s)
stdout, _stderr, _status = Open3.capture3("ogrinfo", "-json", "-ro", "-so", "-al", path.to_s)
stdout
end

# Given an output string from the ogrinfo command, returns
# the vector dataset name.
# @return [String] vector dataset name
def vector_name
match = /(?<=Layer name:\s).*?(?=\n)/.match(doc)
match ? match[0] : ""
doc.dig("layers", 0, "name") || ""
end

# Given an output string from the ogrinfo command, returns
# the ogr driver used to read dataset.
# @return [String] ogr driver name
def driver_name
match = /(?<=driver\s`).*?(?=')/.match(doc)
match ? match[0] : ""
doc.fetch("driverShortName", "")
end

# Given an output string from the ogrinfo command, returns
# the vector geometry type.
# @return [String] vector geom
def vector_geom
match = /(?<=Geometry:\s).*?(?=\n)/.match(doc)
geom = match ? match[0] : ""
geom = doc.dig("layers", 0, "geometryFields", 0, "type") || ""

# Transform OGR-style 'Line String' into GeoJSON 'Line'
geom == "Line String" ? "Line" : geom
geom == "LineString" ? "Line" : geom
end

# Given an output string from the ogrinfo command, returns
# the vector bounding box.
# @return [Hash] vector bounds
def vector_bounds
match = /(?<=Extent:\s).*?(?=\n)/.match(doc)
extent = match ? match[0] : ""
extent = doc.dig("layers", 0, "geometryFields", 0, "extent").map { |c| c.truncate(6) }

# remove parens and spaces, split into array, and assign elements to variables
w, s, e, n = extent.delete(" ").gsub(")-(", ",").delete("(").delete(")").split(",")
{ north: n.to_f, east: e.to_f, south: s.to_f, west: w.to_f }
{ north: extent[3].to_f, east: extent[2].to_f, south: extent[1].to_f, west: extent[0].to_f }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
file_set = valid_file_set
new_file_set = described_class.new(file_set: file_set, persister: persister).characterize(save: false)
expect(new_file_set.original_file.mime_type).to eq ["application/vnd.geo+json"]
expect(new_file_set.original_file.geometry).to eq ["Multi Polygon"]
expect(new_file_set.original_file.geometry).to eq ["MultiPolygon"]
end
end

Expand All @@ -36,7 +36,7 @@
file_set = valid_file_set
new_file_set = described_class.new(file_set: file_set, persister: persister).characterize(save: false)
expect(new_file_set.original_file.mime_type).to eq ["application/vnd.geo+json"]
expect(new_file_set.original_file.geometry).to eq ["Multi Polygon"]
expect(new_file_set.original_file.geometry).to eq ["MultiPolygon"]
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@

RSpec.describe GeoDerivatives::Processors::Vector::Info do
let(:processor) { described_class.new(path) }
let(:path) { "test.tif" }
let(:polygon_info_doc) { file_fixture("files/gdal/ogrinfo_polygon.txt").read }
let(:line_info_doc) { file_fixture("files/gdal/ogrinfo_line.txt").read }

context "when initializing a new info class" do
before do
allow(Open3).to receive(:capture3).and_return([polygon_info_doc, "", ""])
end

it "shells out to ogrinfo and sets the doc variable to the output string" do
expect(processor.doc).to eq(polygon_info_doc)
expect(Open3).to have_received(:capture3).with("ogrinfo", "-ro", "-so", "-al", path.to_s)
end
end
let(:path) { "test.geojson" }
let(:polygon_info_doc) { file_fixture("files/gdal/ogrinfo_polygon.json").read }
let(:line_info_doc) { file_fixture("files/gdal/ogrinfo_line.json").read }

context "with a polygon vector" do
before do
allow(processor).to receive(:doc).and_return(polygon_info_doc)
allow(Open3).to receive(:capture3).and_return([polygon_info_doc, "", ""])
end

describe "#name" do
Expand All @@ -45,7 +34,7 @@
describe "#bounds" do
it "returns bounds hash" do
expect(processor.bounds).to eq(north: 42.408249,
east: -71.052853,
east: -71.052852,
south: 42.347654,
west: -71.163867)
end
Expand All @@ -54,7 +43,7 @@

context "with a line vector" do
before do
allow(processor).to receive(:doc).and_return(line_info_doc)
allow(Open3).to receive(:capture3).and_return([line_info_doc, "", ""])
end

describe "#geom" do
Expand Down
254 changes: 254 additions & 0 deletions spec/fixtures/files/gdal/ogrinfo_line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"description":"/Users/eliotj/Downloads/railroad2004",
"driverShortName":"ESRI Shapefile",
"driverLongName":"ESRI Shapefile",
"layers":[
{
"name":"railroad",
"metadata":{
"":{
"DBF_DATE_LAST_UPDATE":"2004-05-21"
},
"SHAPEFILE":{
"ENCODING_FROM_LDID":"ISO-8859-1",
"LDID_VALUE":"87",
"SOURCE_ENCODING":"ISO-8859-1"
}
},
"geometryFields":[
{
"name":"",
"type":"LineString",
"nullable":true,
"extent":[
913557.58879884903,
125930.64707553876,
1060140.8159581227,
272873.05330644269
],
"coordinateSystem":{
"wkt":"PROJCRS[\"NAD83 / New York Long Island (ftUS)\",\n BASEGEOGCRS[\"NAD83\",\n DATUM[\"North American Datum 1983\",\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4269]],\n CONVERSION[\"SPCS83 New York Long Island zone (US Survey feet)\",\n METHOD[\"Lambert Conic Conformal (2SP)\",\n ID[\"EPSG\",9802]],\n PARAMETER[\"Latitude of false origin\",40.1666666666667,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8821]],\n PARAMETER[\"Longitude of false origin\",-74,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8822]],\n PARAMETER[\"Latitude of 1st standard parallel\",41.0333333333333,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8823]],\n PARAMETER[\"Latitude of 2nd standard parallel\",40.6666666666667,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8824]],\n PARAMETER[\"Easting at false origin\",984250,\n LENGTHUNIT[\"US survey foot\",0.304800609601219],\n ID[\"EPSG\",8826]],\n PARAMETER[\"Northing at false origin\",0,\n LENGTHUNIT[\"US survey foot\",0.304800609601219],\n ID[\"EPSG\",8827]]],\n CS[Cartesian,2],\n AXIS[\"easting (X)\",east,\n ORDER[1],\n LENGTHUNIT[\"US survey foot\",0.304800609601219]],\n AXIS[\"northing (Y)\",north,\n ORDER[2],\n LENGTHUNIT[\"US survey foot\",0.304800609601219]],\n USAGE[\n SCOPE[\"Engineering survey, topographic mapping.\"],\n AREA[\"United States (USA) - New York - counties of Bronx; Kings; Nassau; New York; Queens; Richmond; Suffolk.\"],\n BBOX[40.47,-74.26,41.3,-71.8]],\n ID[\"EPSG\",2263]]",
"projjson":{
"$schema":"https://proj.org/schemas/v0.7/projjson.schema.json",
"type":"ProjectedCRS",
"name":"NAD83 / New York Long Island (ftUS)",
"base_crs":{
"name":"NAD83",
"datum":{
"type":"GeodeticReferenceFrame",
"name":"North American Datum 1983",
"ellipsoid":{
"name":"GRS 1980",
"semi_major_axis":6378137,
"inverse_flattening":298.257222101
}
},
"coordinate_system":{
"subtype":"ellipsoidal",
"axis":[
{
"name":"Geodetic latitude",
"abbreviation":"Lat",
"direction":"north",
"unit":"degree"
},
{
"name":"Geodetic longitude",
"abbreviation":"Lon",
"direction":"east",
"unit":"degree"
}
]
},
"id":{
"authority":"EPSG",
"code":4269
}
},
"conversion":{
"name":"SPCS83 New York Long Island zone (US Survey feet)",
"method":{
"name":"Lambert Conic Conformal (2SP)",
"id":{
"authority":"EPSG",
"code":9802
}
},
"parameters":[
{
"name":"Latitude of false origin",
"value":40.1666666666667,
"unit":"degree",
"id":{
"authority":"EPSG",
"code":8821
}
},
{
"name":"Longitude of false origin",
"value":-74,
"unit":"degree",
"id":{
"authority":"EPSG",
"code":8822
}
},
{
"name":"Latitude of 1st standard parallel",
"value":41.0333333333333,
"unit":"degree",
"id":{
"authority":"EPSG",
"code":8823
}
},
{
"name":"Latitude of 2nd standard parallel",
"value":40.6666666666667,
"unit":"degree",
"id":{
"authority":"EPSG",
"code":8824
}
},
{
"name":"Easting at false origin",
"value":984250,
"unit":{
"type":"LinearUnit",
"name":"US survey foot",
"conversion_factor":0.304800609601219
},
"id":{
"authority":"EPSG",
"code":8826
}
},
{
"name":"Northing at false origin",
"value":0,
"unit":{
"type":"LinearUnit",
"name":"US survey foot",
"conversion_factor":0.304800609601219
},
"id":{
"authority":"EPSG",
"code":8827
}
}
]
},
"coordinate_system":{
"subtype":"Cartesian",
"axis":[
{
"name":"Easting",
"abbreviation":"X",
"direction":"east",
"unit":{
"type":"LinearUnit",
"name":"US survey foot",
"conversion_factor":0.304800609601219
}
},
{
"name":"Northing",
"abbreviation":"Y",
"direction":"north",
"unit":{
"type":"LinearUnit",
"name":"US survey foot",
"conversion_factor":0.304800609601219
}
}
]
},
"scope":"Engineering survey, topographic mapping.",
"area":"United States (USA) - New York - counties of Bronx; Kings; Nassau; New York; Queens; Richmond; Suffolk.",
"bbox":{
"south_latitude":40.47,
"west_longitude":-74.26,
"north_latitude":41.3,
"east_longitude":-71.8
},
"id":{
"authority":"EPSG",
"code":2263
}
},
"dataAxisToSRSAxisMapping":[
1,
2
]
}
}
],
"featureCount":11610,
"fields":[
{
"name":"SOURCEDATE",
"type":"String",
"width":25,
"nullable":true,
"uniqueConstraint":false
},
{
"name":"NAME",
"type":"String",
"width":32,
"nullable":true,
"uniqueConstraint":false
},
{
"name":"SOURCETYPE",
"type":"String",
"width":15,
"nullable":true,
"uniqueConstraint":false
},
{
"name":"SOURCE_ID",
"type":"Integer64",
"width":11,
"nullable":true,
"uniqueConstraint":false
},
{
"name":"MODIFIED",
"type":"String",
"width":25,
"nullable":true,
"uniqueConstraint":false
},
{
"name":"FEAT_CODE",
"type":"Integer",
"width":4,
"nullable":true,
"uniqueConstraint":false
},
{
"name":"FEAT_DESC",
"type":"String",
"width":50,
"nullable":true,
"uniqueConstraint":false
},
{
"name":"EXPORTED",
"type":"String",
"width":12,
"nullable":true,
"uniqueConstraint":false
}
]
}
],
"metadata":{
},
"domains":{
},
"relationships":{
}
}
Loading

0 comments on commit e8fb0dd

Please sign in to comment.