spatial_features 2.10.7 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e62668e5118907b61af7d07274b1e136086f9aa920b8bcbc3a415fd7ce1d7846
4
- data.tar.gz: '090b0a6b28286e52c57079b4e51250c10280b9a185c938b055696607f7ac61be'
3
+ metadata.gz: '035228e67c042788f246694fb215346e72303a7eebe7052cec11a01db0b26ab7'
4
+ data.tar.gz: 4361a0cf3ffa83dfddbe54a35717b2dd31633245fc28438dace60675a2058147
5
5
  SHA512:
6
- metadata.gz: '0293f1342cf5dd4f23c311d4efe555b74e2e0805ce31c35f8e5f711613415a065a23a3105622204358201ef5dac8f60a98bd66de66aebc3da1ed29954f4cca9d'
7
- data.tar.gz: 357e67c32a156fa8fd3ed6d67edfaddde398346babc4f384c3e7ec41b77a5b28cdb215b49021bebe948c27f57ea1f95ac9889caa9f0ba6bce5cf5556b62f4e6a
6
+ metadata.gz: c0e3d96ed8f19a968fbb1ff9fb583091cd7a6565a86d288977baf87eeea6be0412ee52321a24a911baaeb5326d1aa32bf65c176a992d1357e0055c06ad580bb5
7
+ data.tar.gz: 796f292feae7681fd6e4f6b3785421932d9a0a00585b17a5506c25f6399047cb04119e2bcae4db48f1418cace52267821a898fcbd84c1460026bb3dd8cabae26
@@ -40,6 +40,11 @@ class AbstractFeature < ActiveRecord::Base
40
40
  where(:feature_type => 'point')
41
41
  end
42
42
 
43
+ def self.within_distance(lat, lng, distance_in_meters)
44
+ # where("ST_DWithin(features.geog, ST_SetSRID( ST_Point( -71.104, 42.315), 4326)::geography, :distance)", :lat => lat, :lng => lng, :distance => distance_in_meters)
45
+ where("ST_DWithin(features.geog, ST_Point(:lng, :lat), :distance)", :lat => lat, :lng => lng, :distance => distance_in_meters)
46
+ end
47
+
43
48
  def self.area_in_square_meters(geom = 'geom_lowres')
44
49
  current_scope = all.polygons
45
50
  unscoped { connection.select_value(select("ST_Area(ST_Union(#{geom}))").from(current_scope, :features)).to_f }
@@ -14,10 +14,8 @@ class AggregateFeature < AbstractFeature
14
14
  SQL
15
15
 
16
16
  # Remove empty features so ST_COLLECT doesn't choke. This seems to be a difference between PostGIS 2.x and 3.x
17
- # NOTE: ST_CollectionHomogenize is used to normalize geometry representation in order to avoid a segmentation fault
18
- # we were seeing when intersecting complex geometry.
19
17
  self.geog = ActiveRecord::Base.connection.select_value <<~SQL
20
- SELECT COALESCE(ST_CollectionHomogenize(ST_Collect(unnest))::geography, ST_GeogFromText('MULTIPOLYGON EMPTY'))
18
+ SELECT COALESCE(ST_Collect(unnest)::geography, ST_GeogFromText('MULTIPOLYGON EMPTY'))
21
19
  FROM (SELECT unnest(#{feature_array_sql})) AS features
22
20
  WHERE NOT ST_IsEmpty(unnest)
23
21
  SQL
@@ -81,6 +81,15 @@ module SpatialFeatures
81
81
  end
82
82
  end
83
83
 
84
+ def aggregate_features
85
+ type = base_class.to_s # Rails stores polymorphic foreign keys as the base class
86
+ if all == unscoped
87
+ AggregateFeature.where(:spatial_model_type => type)
88
+ else
89
+ AggregateFeature.where(:spatial_model_type => type, :spatial_model_id => all.unscope(:select))
90
+ end
91
+ end
92
+
84
93
  # Returns true if the model stores a hash of the features so we don't need to process the features if they haven't changed
85
94
  def has_spatial_features_hash?
86
95
  column_names.include? 'features_hash'
@@ -131,7 +140,7 @@ module SpatialFeatures
131
140
  scope = scope.select(options[:columns])
132
141
 
133
142
  scope = scope.select("ST_Distance(features.geom, other_features.geom) AS distance_in_meters") if options[:distance]
134
- scope = scope.select("ST_Area(ST_Intersection(features.geom, other_features.geom)) AS intersection_area_in_square_meters") if options[:intersection_area]
143
+ scope = scope.select("ST_Area(ST_Intersection(ST_CollectionExtract(features.geom, 3), ST_CollectionExtract(other_features.geom, 3))) AS intersection_area_in_square_meters") if options[:intersection_area] # Use ST_CollectionExtract to avoid a segfault we've been seeing when intersecting certain geometry
135
144
 
136
145
  return scope
137
146
  end
@@ -183,7 +192,7 @@ module SpatialFeatures
183
192
  end
184
193
 
185
194
  def intersects?(other)
186
- self.class.intersecting(other).exists?(self)
195
+ self.class.unscoped { self.class.intersecting(other).exists?(id) }
187
196
  end
188
197
 
189
198
  def total_intersection_area_percentage(klass)
@@ -67,9 +67,10 @@ module SpatialFeatures
67
67
  private
68
68
 
69
69
  def spatial_feature_imports(import_options, make_valid)
70
- import_options.collect do |data_method, importer_name|
71
- data = send(data_method)
72
- spatial_importer_from_name(importer_name).new(data, :make_valid => make_valid) if data.present?
70
+ import_options.flat_map do |data_method, importer_name|
71
+ Array.wrap(send(data_method)).map do |data|
72
+ spatial_importer_from_name(importer_name).new(data, :make_valid => make_valid) if data.present?
73
+ end
73
74
  end.compact
74
75
  end
75
76
 
@@ -6,22 +6,24 @@ module SpatialFeatures
6
6
  private
7
7
 
8
8
  def each_record(&block)
9
- Nokogiri::XML(@data).css('Placemark').each do |placemark|
10
- name = placemark.css('name').text
11
- metadata = extract_metadata(placemark)
12
-
13
- {'Polygon' => 'POLYGON', 'LineString' => 'LINE', 'Point' => 'POINT'}.each do |kml_type, sql_type|
14
- placemark.css(kml_type).each do |placemark|
15
- next if blank_placemark?(placemark)
16
-
17
- yield OpenStruct.new(:feature_type => sql_type, :geog => geom_from_kml(placemark), :name => name, :metadata => metadata)
9
+ {'Polygon' => 'POLYGON', 'LineString' => 'LINE', 'Point' => 'POINT'}.each do |kml_type, sql_type|
10
+ Nokogiri::XML(@data).css(kml_type).each do |feature|
11
+ if placemark = feature.ancestors('Placemark').first
12
+ name = placemark.css('name').text
13
+ metadata = extract_metadata(placemark)
14
+ else
15
+ metadata = {}
18
16
  end
17
+
18
+ next if blank_feature?(feature)
19
+
20
+ yield OpenStruct.new(:feature_type => sql_type, :geog => geom_from_kml(feature), :name => name, :metadata => metadata)
19
21
  end
20
22
  end
21
23
  end
22
24
 
23
- def blank_placemark?(placemark)
24
- placemark.css('coordinates').text.blank?
25
+ def blank_feature?(feature)
26
+ feature.css('coordinates').text.blank?
25
27
  end
26
28
 
27
29
  def geom_from_kml(kml)
@@ -1,3 +1,3 @@
1
1
  module SpatialFeatures
2
- VERSION = "2.10.7"
2
+ VERSION = "2.12.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spatial_features
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.7
4
+ version: 2.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Wallace
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-05-23 00:00:00.000000000 Z
12
+ date: 2020-06-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails