Class: NSWTopo::ArcGIS::Service

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/nswtopo/gis/arcgis/service.rb

Constant Summary collapse

SERVICE =
/^(?:MapServer|FeatureServer|ImageServer)$/
InvalidURLError =
Class.new RuntimeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Service

Returns a new instance of Service.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nswtopo/gis/arcgis/service.rb', line 23

def initialize(url)
  uri, service_path, @id = Service.check_uri url
  raise InvalidURLError, "invalid ArcGIS server URL: %s" % url unless uri
  @connection = Connection.new uri, service_path
  @service = get_json ""

  @projection = case
  when wkt  = @service.dig("spatialReference", "wkt") then Projection.new(wkt)
  when wkid = @service.dig("spatialReference", "latestWkid") then Projection.new("EPSG:#{wkid}")
  when wkid = @service.dig("spatialReference", "wkid") then Projection.new("EPSG:#{wkid == 102100 ? 3857 : wkid}")
  else raise "no spatial reference found: #{uri}"
  end
end

Instance Attribute Details

#projectionObject (readonly)

Returns the value of attribute projection.



40
41
42
# File 'lib/nswtopo/gis/arcgis/service.rb', line 40

def projection
  @projection
end

Class Method Details

.===(string) ⇒ Object



18
19
20
21
# File 'lib/nswtopo/gis/arcgis/service.rb', line 18

def self.===(string)
  uri, service_path, id = check_uri string
  uri != nil
end

.check_uri(url) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/nswtopo/gis/arcgis/service.rb', line 7

def self.check_uri(url)
  uri = URI.parse url
  return unless URI::HTTP === uri
  return unless uri.path
  instance, (id, *) = uri.path.split(?/).slice_after(SERVICE).take(2)
  return unless SERVICE === instance&.last
  return unless !id || id =~ /^\d+$/
  return uri, instance.join(?/), id
rescue URI::Error
end

Instance Method Details

#layer(id: @id, **options) ⇒ Object



42
43
44
# File 'lib/nswtopo/gis/arcgis/service.rb', line 42

def layer(id: @id, **options)
  Layer.new self, id: id, **options
end

#layer_infoObject



46
47
48
49
50
51
52
53
54
# File 'lib/nswtopo/gis/arcgis/service.rb', line 46

def layer_info
  children = @service["layers"].group_by do |layer|
    layer["parentLayerId"] || -1
  end
  tree = lambda do |layer|
    [layer.values_at("id", "name").join(": "), children.fetch(layer["id"], []).map(&tree)]
  end
  children[-1].map(&tree)
end