Module: NSWTopo::ArcGIS::Query

Defined in:
lib/nswtopo/gis/arcgis/layer/query.rb

Constant Summary collapse

UniqueFieldError =
Class.new RuntimeError

Instance Method Summary collapse

Instance Method Details

#base_query(**options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nswtopo/gis/arcgis/layer/query.rb', line 6

def base_query(**options)
  case
  when @unique
    raise UniqueFieldError
  when @geometry
    raise "polgyon geometry required" unless @geometry.polygon?
    options[:geometry] = { rings: @geometry.reproject_to(projection).coordinates.map(&:reverse) }.to_json
    options[:geometryType] = "esriGeometryPolygon"
    options[:where] = join_clauses(*@where) if @where
  when @where
    options[:where] = join_clauses(*@where)
  else
    oid_field = @layer["fields"].find do |field|
      field["type"] == "esriFieldTypeOID"
    end&.fetch("name")
    options[:where] = oid_field ? "#{oid_field} IS NOT NULL" : "1=1"
  end
  options
end

#countObject



26
27
28
# File 'lib/nswtopo/gis/arcgis/layer/query.rb', line 26

def count
  @count ||= get_json("#{@id}/query", **base_query, returnCountOnly: true).dig("count")
end

#pages(per_page) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nswtopo/gis/arcgis/layer/query.rb', line 30

def pages(per_page)
  objectids = get_json("#{@id}/query", **base_query, returnIdsOnly: true)["objectIds"] || []
  @count = objectids.count
  return [GeoJSON::Collection.new(projection: projection, name: @name)].each if @count.zero?

  @fields ||= @layer["fields"].select do |field|
    Layer::FIELD_TYPES === field["type"]
  end.map do |field|
    field["name"]
  end

  Enumerator.new do |yielder|
    out_fields = [*@fields, *extra_field].join ?,
    while objectids.any?
      begin
        get_json "#{@id}/query", outFields: out_fields, objectIds: objectids.take(per_page).join(?,)
      rescue Connection::Error
        (per_page /= 2) > 0 ? retry : raise
      end.fetch("features", []).filter_map do |feature|
        next unless geometry = feature["geometry"]
        properties = feature.fetch("attributes", {})

        case @geometry_type
        when "esriGeometryPoint"
          point = geometry.values_at "x", "y"
          next unless point.all?
          next GeoJSON::Point[point, properties]
        when "esriGeometryMultipoint"
          points = geometry["points"]
          next unless points&.any?
          next GeoJSON::MultiPoint[points.transpose.take(2).transpose, properties]
        when "esriGeometryPolyline"
          raise "ArcGIS curve geometries not supported" if geometry.key? "curvePaths"
          paths = geometry["paths"]
          next unless paths&.any?
          next GeoJSON::LineString[paths[0], properties] if @mixed && paths.one?
          next GeoJSON::MultiLineString[paths, properties]
        when "esriGeometryPolygon"
          raise "ArcGIS curve geometries not supported" if geometry.key? "curveRings"
          rings = geometry["rings"]
          next unless rings&.any?
          polys = GeoJSON::MultiLineString[rings.map(&:reverse), properties].to_multipolygon
          next @mixed && polys.one? ? polys.first : polys
        else
          raise "unsupported ArcGIS geometry type: #{@geometry_type}"
        end
      end.tap do |features|
        yielder << GeoJSON::Collection.new(projection: projection, features: features, name: @name)
      end
      objectids.shift per_page
    end
  end
end