Module: NSWTopo::Shapefile

Included in:
Feature, Relief
Defined in:
lib/nswtopo/gis/shapefile.rb

Constant Summary collapse

Error =
Class.new RuntimeError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.===(path) ⇒ Object



5
6
7
8
9
10
# File 'lib/nswtopo/gis/shapefile.rb', line 5

def self.===(path)
  OS.ogrinfo "-ro", "-so", path
  true
rescue OS::Error
  false
end

Instance Method Details

#shapefile_layer(shapefile_path, where: nil, sql: nil, layer: nil, margin: {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nswtopo/gis/shapefile.rb', line 12

def shapefile_layer(shapefile_path, where: nil, sql: nil, layer: nil, margin: {})
  raise "#{@source}: can't specify both SQL and where clause" if sql && where
  raise "#{@source}: can't specify both SQL and layer name" if sql && layer
  sql   = ["-sql", sql] if sql
  where = ["-where", "(" << Array(where).join(") AND (") << ")"] if where
  srs   = ["-t_srs", @map.projection]
  spat  = ["-spat", *@map.bounds(margin: margin).transpose.flatten, "-spat_srs", @map.projection]
  misc  = %w[-mapFieldType Date=Integer,DateTime=Integer -dim XY]
  json = OS.ogr2ogr *(sql || where), *srs, *spat, *misc, "-f", "GeoJSON", "-lco", "RFC7946=NO", "/vsistdout/", shapefile_path, *layer
  GeoJSON::Collection.load json, @map.projection
end