ea-area_lookup 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/lib/ea/area_lookup/configuration.rb +2 -2
- data/lib/ea/area_lookup/finders.rb +23 -10
- data/lib/ea/area_lookup/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be99b0c83704e166b6c0d61131c11695e35c1771
|
4
|
+
data.tar.gz: 18d26c91a81a300752ea23403784222ce167aa48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ef9beda87b4414e3439a36afb5cc052dabb0b067435ddbbc5731cbc5f7a9283677e06da30c9d06150de0511a0bd0fc3a2f8483626f24626f124fa4a13c6d8b6
|
7
|
+
data.tar.gz: 039e95846368aba23844d98038022de068f88ba481f19976dee60d57ebd330054c3f9cc1dca8e4fe8fc830978a4623252c0d3adf88248dec8c9de002bcfdfc8c
|
data/README.md
CHANGED
@@ -31,7 +31,7 @@ Create an intializer e.g. `config/initializers/area_lookup.rb`
|
|
31
31
|
|
32
32
|
```ruby
|
33
33
|
EA::AreaLookup.configure do |config|
|
34
|
-
config.
|
34
|
+
config.area_api_url = "http://admin-area-api-base-url"
|
35
35
|
end
|
36
36
|
```
|
37
37
|
|
@@ -40,7 +40,8 @@ Now you can do the following
|
|
40
40
|
```ruby
|
41
41
|
coords = EA::AreaLookup::Coordinates.new(easting: 123, northing: 456)
|
42
42
|
(or EA::AreaLookup::Coordinates.new(x: 123, y: 456))
|
43
|
-
result = EA::AreaLookup.
|
43
|
+
result = EA::AreaLookup.find_admin_area_by_coordinates(coords)
|
44
|
+
# or result = EA::AreaLookup.find_water_management_area_by_coordinates(coords)
|
44
45
|
p result
|
45
46
|
=> {:area_id=>"XX", :code=>"WESSEX", :area_name=>"Wessex", :short_name=>"Wessex", :long_name=>"Wessex"}
|
46
47
|
```
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module EA
|
2
2
|
module AreaLookup
|
3
3
|
class Configuration
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :area_api_url
|
5
5
|
|
6
6
|
def initialize
|
7
|
-
@
|
7
|
+
@area_api_url ||= "http://environment.data.gov.uk/ds/wfs"
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -4,11 +4,21 @@ require "open-uri"
|
|
4
4
|
module EA
|
5
5
|
module AreaLookup
|
6
6
|
module Finders
|
7
|
-
|
7
|
+
|
8
|
+
def find_admin_area_by_coordinates(coords)
|
8
9
|
validate_config!
|
9
10
|
return nil unless coords && coords.valid?
|
10
|
-
|
11
|
-
|
11
|
+
typename = "ea-wfs-area_public_face_inspire"
|
12
|
+
xml = fetch_area_from_api(coords, typename)
|
13
|
+
parse_xml(xml, typename)
|
14
|
+
end
|
15
|
+
|
16
|
+
def find_water_management_area_by_coordinates(coords)
|
17
|
+
validate_config!
|
18
|
+
return nil unless coords && coords.valid?
|
19
|
+
typename = "ea-wfs-area_water_management_inspire"
|
20
|
+
xml = fetch_area_from_api(coords, typename)
|
21
|
+
parse_xml(xml, typename)
|
12
22
|
end
|
13
23
|
|
14
24
|
private
|
@@ -20,22 +30,25 @@ module EA
|
|
20
30
|
REQUEST: "GetFeature",
|
21
31
|
MSVER: 6,
|
22
32
|
SRS: "EPSG:27700",
|
23
|
-
|
33
|
+
|
24
34
|
propertyName: "long_name,short_name,code,area_id,area_name",
|
25
35
|
Filter: ""
|
26
36
|
}.freeze
|
27
37
|
|
28
38
|
def api_url
|
29
|
-
EA::AreaLookup.config.
|
39
|
+
EA::AreaLookup.config.area_api_url
|
30
40
|
end
|
31
41
|
|
32
42
|
def validate_config!
|
33
43
|
return if api_url
|
34
|
-
raise EA::AreaLookup::InvalidConfigError, "Missing
|
44
|
+
raise EA::AreaLookup::InvalidConfigError, "Missing area_api_url"
|
35
45
|
end
|
36
46
|
|
37
|
-
def
|
38
|
-
params = DEFAULT_API_PARAMS
|
47
|
+
def fetch_area_from_api(coords, typename)
|
48
|
+
params = DEFAULT_API_PARAMS
|
49
|
+
.merge(Filter: "(#{filter_xml(coords)})")
|
50
|
+
.merge(TYPENAME: typename)
|
51
|
+
|
39
52
|
full_url = %(#{api_url}?#{params.map { |k, v| "#{k}=#{v}" }.join('&')})
|
40
53
|
open full_url, proxy: ENV["http_proxy"]
|
41
54
|
rescue => e
|
@@ -50,12 +63,12 @@ module EA
|
|
50
63
|
"</Intersects></Filter>"
|
51
64
|
end
|
52
65
|
|
53
|
-
def parse_xml(response)
|
66
|
+
def parse_xml(response, typename)
|
54
67
|
xml = Nokogiri::XML response
|
55
68
|
validate_xml(xml)
|
56
69
|
|
57
70
|
result = %i(area_id code area_name short_name long_name).each_with_object({}) do |path, hash|
|
58
|
-
hash[path] = xml.xpath("//gml:featureMember/ms
|
71
|
+
hash[path] = xml.xpath("//gml:featureMember/ms:#{typename}/ms:#{path}").text
|
59
72
|
end
|
60
73
|
result.tap { |h| EA::AreaLookup.logger.debug(h) }
|
61
74
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ea-area_lookup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Digital Services Team, EnvironmentAgency
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|