rp_clustering-rgeo-activerecord 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzlmNTQwZDMwYzkxOGNkMDc5MzhhOTNiYWExYTQwNWJjODljODdiZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NzAyYzFmOGRmZjUxNzIwNjk0Yjg2YzRlY2Q2MmNkMWIyNGUwOTAzMw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmExOTU0ZTJjNGNmNTA0Nzg3ZTlmNDZhYjcwMmJlYTIwOGViOGFhMjY5YmM1
|
10
|
+
MjU3YmYzZGVlMTU0NGZkOTM2OTBlMmVlNzI0MjM3MjEzNTU3NzA1NzcyODJh
|
11
|
+
NGMxODQzMDQ3NzU5ZGE2ZmYyMzhjMDg5M2Y1NmZhOGY2ZWEwNzk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NmRkNTM2NmNiNTZhYmM0MjU0OGQ4MzQwZGRlNmY1YmNkNmQ0NjMxMzU0Njc1
|
14
|
+
MGE3MDNmOTljYzhkNTljYmRkZThhMGJiYWQ1YzQ4MzljNzhjODYwMThlYzUw
|
15
|
+
MjNjYTk3NDZjNzQxMjcyMDE3NmQzMWQ0YmNjYmI1ZWNkMjJkZmQ=
|
data/README.md
CHANGED
@@ -34,7 +34,7 @@ Or install it yourself as:
|
|
34
34
|
|
35
35
|
## Usage
|
36
36
|
|
37
|
-
### Added in Version 0.0.3
|
37
|
+
### Added in Version 0.0.3 (Fixed in 0.0.4)
|
38
38
|
|
39
39
|
This version allows for the "on the fly" use of a ST_SnapToGrid clustering function.
|
40
40
|
The function is added to ActiveRecord::Base (Models). The function is:
|
@@ -53,7 +53,7 @@ The function is added to ActiveRecord::Base (Models). The function is:
|
|
53
53
|
# Options:
|
54
54
|
#
|
55
55
|
# [:grid_size] if set, will be used to create the cluster. The clustering
|
56
|
-
# works rougly like this; all geometries within 'grid_size'
|
56
|
+
# works rougly like this; all geometries within 'grid_size' of each other
|
57
57
|
# will be pulled together to form a single cluster. For a detailed
|
58
58
|
# explanation, please see the PostGIS docs for ST_SnapToGrid.
|
59
59
|
#
|
@@ -120,7 +120,7 @@ RGeo::GeoJSON.encode(feature_collection)
|
|
120
120
|
# make outliers (small clusters) bright red, or you could vary the size of the
|
121
121
|
# cluster centroid based on the size of cluster.
|
122
122
|
#
|
123
|
-
# Ideally, you
|
123
|
+
# Ideally, you would vary your +grid_size+ based on the user's view port.
|
124
124
|
# For example, you could set it to fixed values based on the user's zoom level.
|
125
125
|
# You could dynamically generate it based on some fraction of the user's view port bbox.
|
126
126
|
|
@@ -14,8 +14,7 @@ module RPClustering
|
|
14
14
|
|
15
15
|
module ClassMethods
|
16
16
|
|
17
|
-
# Cluster using the PostGIS function ST_SnapToGrid
|
18
|
-
# -------------------------------------------------
|
17
|
+
# === Cluster using the PostGIS function ST_SnapToGrid
|
19
18
|
#
|
20
19
|
# attr_to_cluster is the name of attribute to be clustered (a symbol).
|
21
20
|
# The attribute should be geometry attribute.
|
@@ -23,39 +22,45 @@ module RPClustering
|
|
23
22
|
# Use the options Hash to define what cluster properties you would
|
24
23
|
# like returned.
|
25
24
|
#
|
26
|
-
# Options:
|
25
|
+
# == Options:
|
27
26
|
#
|
28
27
|
# [:grid_size] if set, will be used to create the cluster. The clustering
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
28
|
+
# works rougly like this; all geometries within 'grid_size' of each other
|
29
|
+
# will be pulled together to form a single cluster. For a detailed
|
30
|
+
# explanation, please see the PostGIS docs for ST_SnapToGrid.
|
32
31
|
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
32
|
+
# If no +:grid_size+ is given, clusters will consist of all 'equal'
|
33
|
+
# geometries. E.g. all points at the same
|
34
|
+
# position (x,y) will be pulled together to form a single cluster.
|
35
|
+
# This is actually just a Group By of your +attr_to_cluster+.
|
37
36
|
#
|
38
37
|
# [:cluster_geometry_count] if set to true, the query will select, for
|
39
|
-
#
|
38
|
+
# each cluster, the number of geometries in the cluster.
|
40
39
|
#
|
41
40
|
# [:cluster_geometry_count_as] the name to select the
|
42
|
-
#
|
41
|
+
# cluster_geometry_count as, defaults
|
42
|
+
# to "cluster_geometry_count".
|
43
43
|
#
|
44
44
|
# [:cluster_centroid] if set to true, the query will select, for
|
45
|
-
#
|
46
|
-
#
|
45
|
+
# each cluster, the cluster centroid. The
|
46
|
+
# cluster_centroid returned will be a WKT string.
|
47
47
|
#
|
48
|
-
# [:cluster_centroid_as] the name to select the
|
49
|
-
#
|
48
|
+
# [:cluster_centroid_as] the name to select the cluster_centroid
|
49
|
+
# as, defaults to "cluster_centroid".
|
50
50
|
#
|
51
51
|
# [:cluster_minimum_bounding_circle] if set to true, the query will select,
|
52
|
-
#
|
53
|
-
#
|
52
|
+
# for each cluster, the minimum
|
53
|
+
# bouding circle. The
|
54
|
+
# cluster_minimum_bounding_circle
|
55
|
+
# will be a WKT string.
|
54
56
|
#
|
55
57
|
# [:cluster_minimum_bounding_circle_as] the name to select the
|
56
|
-
#
|
58
|
+
# cluster_minimum_bounding_circle as,
|
59
|
+
# defaults to "cluster_minimum_bounding_circle"
|
57
60
|
#
|
58
|
-
# Note
|
61
|
+
# == Note
|
62
|
+
#
|
63
|
+
# Using the options hash, you must 'select' at least one attribute,
|
59
64
|
# else this method will raise an ArgumentError.
|
60
65
|
#
|
61
66
|
|
@@ -129,7 +134,7 @@ module RPClustering
|
|
129
134
|
|
130
135
|
select(
|
131
136
|
arel_table.st_astext(
|
132
|
-
arel_attr.st_collect
|
137
|
+
arel_table.st_centroid(arel_attr.st_collect)
|
133
138
|
).as(as)
|
134
139
|
)
|
135
140
|
end
|
data/test/test_clustering.rb
CHANGED
@@ -103,6 +103,32 @@ module RPClustering
|
|
103
103
|
|
104
104
|
end
|
105
105
|
|
106
|
+
def test_cluster_centroid_location_is_where_we_expect_it
|
107
|
+
arel_klass = populate_ar_class(:latlon_point)
|
108
|
+
|
109
|
+
points_generated = 0
|
110
|
+
(-5..5).each do |lng|
|
111
|
+
(-5..5).each do |lat|
|
112
|
+
obj = arel_klass.new
|
113
|
+
obj.latlon = @geographic_factory.point(lng, lat)
|
114
|
+
obj.save!
|
115
|
+
points_generated+=1
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
res = arel_klass.cluster_by_st_snap_to_grid(:latlon, grid_size: 100, cluster_geometry_count: true, cluster_centroid: true)
|
120
|
+
clusters = res.all
|
121
|
+
total_clusters = clusters.count()
|
122
|
+
|
123
|
+
# We should have one cluster.
|
124
|
+
assert_equal(1, total_clusters, "we should have one cluster with such a large grid_size")
|
125
|
+
|
126
|
+
centroid_point = @geographic_factory.parse_wkt(clusters.first["cluster_centroid"])
|
127
|
+
|
128
|
+
assert_equal(0, centroid_point.x, "we expect the centroid to be at 0,0")
|
129
|
+
assert_equal(0, centroid_point.y, "we expect the centroid to be at 0,0")
|
130
|
+
end
|
131
|
+
|
106
132
|
end
|
107
133
|
end
|
108
134
|
end
|