terragona 0.2.0 → 0.2.1

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bacfcba592bdf78f9891f8065d8f9e8172fe049c
4
- data.tar.gz: 9c8baebb11ad0b66b27635bab3c1c2bd918e4902
3
+ metadata.gz: 8960cea1763219e0706e07cd4f972d62c57a87ff
4
+ data.tar.gz: 0377f3156e42d39426927b353aaf68bade666536
5
5
  SHA512:
6
- metadata.gz: 21c854dbc451bf049243d910ee0276c4214583a1f97695f81bd7ec522058c53fe0577a284c3fe3f0c7740442f795702181d77acd5f7d9690d3adb90f02a54c3d
7
- data.tar.gz: b693d2a52d5481d861fe279b158b41b9d31da88cde5a4155053146d743697c96bcc1f2b1cda3c6c1f949c8d67668cea3685bc3be0ad7d76d7627dbcfbe348fc6
6
+ metadata.gz: a841fbf0d6f63e54492ed030388d85075a5e92199cf44a3443e89c39db13b4d8b076ae478fff06d6e0fcbdfd4681710006e66953794e4127729ca360a69ccbec
7
+ data.tar.gz: 523101fea46f423189c81b62cabc80ef3fd46ba30a414547542b662c909e283cf733450db0494f3d7845eef549e4aa70b677859b9ced0cd7dc8123231923ad05
data/README.md CHANGED
@@ -153,6 +153,9 @@ allow_holes Can the polygons have holes? Default: false.
153
153
  max_distance_ratio Points distant more than this ratio times from the average
154
154
  distance between points are not considered. Default: 1.6.
155
155
  minimal_polygon_points Minimal number of points to build a polygon.
156
+ force_homogeneity Uses max_distance_ratio also to compare with the avg distance
157
+ between points of all the other polygons of the same family level.
158
+ This helps to discard outliers. The result are homogeneous polygons.
156
159
  dont_create_polygons (boolean) Default: false.
157
160
  table Table where polygons are saved. This option is overriden
158
161
  by args of create_polygons_family method.
@@ -1,5 +1,6 @@
1
1
  require 'geokit'
2
2
  require 'sequel'
3
+ require_relative 'stats'
3
4
 
4
5
  module Terragona
5
6
  class ConcaveHull
@@ -10,6 +11,7 @@ module Terragona
10
11
  @allow_holes = options[:allow_holes]
11
12
  @allow_holes = false if @allow_holes.nil?
12
13
  @max_distance_ratio = options[:max_distance_ratio] || 1.6
14
+ @force_homogeneity = options[:force_homogeneity]
13
15
 
14
16
  db_options={
15
17
  :database=> options[:db_name],
@@ -21,7 +23,7 @@ module Terragona
21
23
  }
22
24
 
23
25
  @db = Sequel.postgres(db_options)
24
-
26
+ @all_means = []
25
27
  create_table
26
28
  end
27
29
 
@@ -45,10 +47,11 @@ module Terragona
45
47
  end
46
48
 
47
49
  private
50
+
48
51
  def create_table
49
52
  @db << "DROP TABLE IF EXISTS #{@table};"
50
53
  @db << "CREATE TABLE #{@table} (id BIGINT PRIMARY KEY, name TEXT, count INT);"
51
- @db << "SELECT AddGeometryColumn('#{@table}', 'geometry',#{@projection}, 'POLYGON', 2);"
54
+ @db << "SELECT AddGeometryColumn('#{@table}', 'geometry', #{@projection}, 'POLYGON', 2);"
52
55
  end
53
56
 
54
57
  def create_concave_hull(query, tags, count, id)
@@ -60,20 +63,27 @@ module Terragona
60
63
  end
61
64
  end
62
65
 
63
- def filter_points_by_distance(points)
66
+ def filter_points_by_distance(points)
64
67
  random_points = points.count > 200 ? (0..200).map {|e|
65
68
  points[rand(points.count)]
66
69
  }.uniq : points
67
-
70
+
68
71
  distances = points.map {|p0|
69
72
  sum = random_points.map {|pf|
70
- Geokit::LatLng.new(p0[:y],p0[:x]).distance_to(Geokit::LatLng.new(pf[:y],pf[:x]))
71
- }
72
- {:y=>p0[:y],:x=>p0[:x],:average=>average(sum)}
73
+ if p0[:y] != pf[:y] and p0[:x] != pf[:x]
74
+ Geokit::LatLng.new(p0[:y],p0[:x]).distance_to(Geokit::LatLng.new(pf[:y],pf[:x]))
75
+ end
76
+ }.compact
77
+ {:y=>p0[:y],:x=>p0[:x],:mean=> sum.mean}
73
78
  }
74
- average_distance=average(distances.map{|d| d[:average]})
79
+
80
+ mean = distances.map{|d| d[:mean]}.compact.mean
81
+ @all_means.push mean
82
+ mean_of_means = @force_homogeneity ? @all_means.compact.mean : nil
83
+
75
84
  distances.map {|d|
76
- d if d[:average] <= average_distance * @max_distance_ratio
85
+ next unless d[:mean]
86
+ d if (d[:mean]/[mean,mean_of_means].compact.min) < @max_distance_ratio
77
87
  }.compact
78
88
  end
79
89
 
@@ -83,10 +93,6 @@ module Terragona
83
93
  }.join(',')
84
94
  end
85
95
 
86
- def average(arr)
87
- arr.inject{ |sum, el| sum + el }.to_f / arr.size
88
- end
89
-
90
96
  def clean_str(str)
91
97
  str.to_s.gsub("'",' ')
92
98
  end
@@ -0,0 +1,25 @@
1
+ # Taken from:
2
+ # http://stackoverflow.com/questions/7749568/how-can-i-do-standard-deviation-in-ruby
3
+
4
+ module Enumerable
5
+
6
+ def sum
7
+ return self.inject(0){|accum, i| accum + i }
8
+ end
9
+
10
+ def mean
11
+ return if self.empty?
12
+ return self.sum / self.length.to_f
13
+ end
14
+
15
+ def sample_variance
16
+ m = self.mean
17
+ sum = self.inject(0){|accum, i| accum + (i - m) ** 2 }
18
+ return sum / (self.length - 1).to_f
19
+ end
20
+
21
+ def standard_deviation
22
+ return Math.sqrt(self.sample_variance)
23
+ end
24
+
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Terragona
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/terragona.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Terragona::VERSION
9
9
  spec.authors = ["Bruno Salerno"]
10
10
  spec.email = ["br.salerno@gmail.com"]
11
- spec.description = %q{Create polygons for geonames places}
12
- spec.summary = %q{Use API or dumps as input, draw polygons, and store them in a Postgres/Postgis db}
11
+ spec.description = %q{Create polygons from geonames places and other sources}
12
+ spec.summary = %q{Use API, Dumps or a CSV file as input, draw polygons, and store them in a Postgres/Postgis db}
13
13
  spec.homepage = "https://github.com/BrunoSalerno/terragona"
14
14
  spec.license = "MIT"
15
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terragona
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Salerno
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-12 00:00:00.000000000 Z
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -136,7 +136,7 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
- description: Create polygons for geonames places
139
+ description: Create polygons from geonames places and other sources
140
140
  email:
141
141
  - br.salerno@gmail.com
142
142
  executables: []
@@ -153,6 +153,7 @@ files:
153
153
  - lib/terragona/concave_hull.rb
154
154
  - lib/terragona/generic.rb
155
155
  - lib/terragona/geonames.rb
156
+ - lib/terragona/stats.rb
156
157
  - lib/terragona/version.rb
157
158
  - terragona.gemspec
158
159
  homepage: https://github.com/BrunoSalerno/terragona
@@ -178,6 +179,6 @@ rubyforge_project:
178
179
  rubygems_version: 2.2.2
179
180
  signing_key:
180
181
  specification_version: 4
181
- summary: Use API or dumps as input, draw polygons, and store them in a Postgres/Postgis
182
- db
182
+ summary: Use API, Dumps or a CSV file as input, draw polygons, and store them in a
183
+ Postgres/Postgis db
183
184
  test_files: []