Class: AggregateFeature
- Inherits:
-
AbstractFeature
- Object
- ActiveRecord::Base
- AbstractFeature
- AggregateFeature
- Defined in:
- app/models/aggregate_feature.rb
Constant Summary
Constants inherited from AbstractFeature
AbstractFeature::FEATURE_TYPES
Instance Method Summary collapse
-
#refresh ⇒ Object
Aggregate the features for the spatial model into a single feature.
Methods inherited from AbstractFeature
area_in_square_meters, cache_derivatives, #cache_derivatives, cache_key, #envelope, #feature_bounds, #geojson, geojson, intersecting, invalid, #kml, lines, #make_valid?, points, polygons, total_intersection_area_in_square_meters, valid, with_metadata
Instance Method Details
#refresh ⇒ Object
Aggregate the features for the spatial model into a single feature
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/models/aggregate_feature.rb', line 7 def refresh feature_array_sql = " ARRAY[\n (\#{features.select('ST_Multi(ST_Union(ST_CollectionExtract(geog::geometry, 1)))').to_sql}),\n (\#{features.select('ST_Multi(ST_Union(ST_CollectionExtract(geog::geometry, 2)))').to_sql}),\n (\#{features.select('ST_Multi(ST_Union(ST_CollectionExtract(geog::geometry, 3)))').to_sql})\n ]\n SQL\n\n # Remove empty features so ST_COLLECT doesn't choke. This seems to be a difference between PostGIS 2.x and 3.x\n compacted_feature_array_sql = <<~SQL\n array_remove(\n array_remove(\n array_remove(\#{feature_array_sql},\n ST_GeomFromText('MultiPoint EMPTY')),\n ST_GeomFromText('MultiLinestring EMPTY')),\n ST_GeomFromText('MultiPolygon EMPTY'))\n SQL\n\n self.geog = ActiveRecord::Base.connection.select_value <<~SQL\n SELECT ST_Collect(\#{compacted_feature_array_sql})::geography\n SQL\n self.save!\nend\n" |