libgd-gis 0.4.8 → 0.4.9
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 +4 -4
- data/lib/gd/gis/layer_geojson.rb +49 -17
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9b617161c82e9481b5787df8379b18b0f5bfc267dc62ae1d6979d0cacd92881d
|
|
4
|
+
data.tar.gz: ec8b6d1c651b74dee77266186e5e5b767e7133f863bb51d9dded95b549c7cedc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8f9a191f2d884a477557cdc4e5efe69545cdf43f78528056f4b7f82ec1500dedb8015374fed929bb4035c1d0f27ce414aeaff018e2d4eb36802399408f5e41c1
|
|
7
|
+
data.tar.gz: bb24e7dd4c41e7783d41e97ed7f6ddc8b01a8a0372c9ecdbb182666e977ed365856a10a35b7faf88a3dbbc35aaf52ff33b2e8090c915f3366f5ad32ad3af91f5
|
data/lib/gd/gis/layer_geojson.rb
CHANGED
|
@@ -26,19 +26,9 @@ module GD
|
|
|
26
26
|
# @raise [JSON::ParserError] if the file is invalid JSON
|
|
27
27
|
# @raise [Errno::ENOENT] if the file does not exist
|
|
28
28
|
def self.load(source)
|
|
29
|
+
data = normalize_source(source)
|
|
29
30
|
|
|
30
|
-
data
|
|
31
|
-
when String
|
|
32
|
-
if source.strip.start_with?("{", "[")
|
|
33
|
-
JSON.parse(source)
|
|
34
|
-
else
|
|
35
|
-
JSON.parse(File.read(source))
|
|
36
|
-
end
|
|
37
|
-
when Hash
|
|
38
|
-
source
|
|
39
|
-
else
|
|
40
|
-
raise ArgumentError, "Unsupported GeoJSON source"
|
|
41
|
-
end
|
|
31
|
+
validate_geojson!(data)
|
|
42
32
|
|
|
43
33
|
# 1) Detect CRS
|
|
44
34
|
crs_name = data["crs"]&.dig("properties", "name")
|
|
@@ -48,13 +38,55 @@ module GD
|
|
|
48
38
|
ontology = Ontology.new
|
|
49
39
|
|
|
50
40
|
# 3) Normalize geometries + classify
|
|
51
|
-
data["features"].map do |
|
|
52
|
-
|
|
41
|
+
data["features"].map do |feature|
|
|
42
|
+
geometry = feature["geometry"]
|
|
43
|
+
properties = feature["properties"] || {}
|
|
44
|
+
|
|
45
|
+
raise ArgumentError, "Missing geometry" unless geometry
|
|
46
|
+
raise ArgumentError, "Missing geometry type" unless geometry["type"]
|
|
47
|
+
raise ArgumentError, "Missing coordinates" unless geometry["coordinates"]
|
|
48
|
+
|
|
49
|
+
normalize_geometry!(geometry, normalizer)
|
|
50
|
+
|
|
53
51
|
layer = ontology.classify(
|
|
54
|
-
|
|
55
|
-
geometry_type:
|
|
52
|
+
properties,
|
|
53
|
+
geometry_type: geometry["type"]
|
|
56
54
|
)
|
|
57
|
-
|
|
55
|
+
|
|
56
|
+
Feature.new(geometry, properties, layer)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.normalize_source(source)
|
|
61
|
+
case source
|
|
62
|
+
when Hash
|
|
63
|
+
source
|
|
64
|
+
|
|
65
|
+
when String
|
|
66
|
+
begin
|
|
67
|
+
JSON.parse(source)
|
|
68
|
+
rescue JSON::ParserError
|
|
69
|
+
raise ArgumentError, "File not found: #{source}" unless File.exist?(source)
|
|
70
|
+
|
|
71
|
+
JSON.parse(File.read(source))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
else
|
|
75
|
+
raise ArgumentError, "Unsupported GeoJSON source: #{source.class}"
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.validate_geojson!(data)
|
|
80
|
+
unless data.is_a?(Hash)
|
|
81
|
+
raise ArgumentError, "GeoJSON must be an object"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
unless data["type"] == "FeatureCollection"
|
|
85
|
+
raise ArgumentError, "Only FeatureCollection is supported"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
unless data["features"].is_a?(Array)
|
|
89
|
+
raise ArgumentError, "GeoJSON must contain features array"
|
|
58
90
|
end
|
|
59
91
|
end
|
|
60
92
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: libgd-gis
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Germán Alberto Giménez Silva
|
|
@@ -15,7 +15,7 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 0.
|
|
18
|
+
version: 0.3.0
|
|
19
19
|
- - "<"
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
21
|
version: 0.4.0
|
|
@@ -25,7 +25,7 @@ dependencies:
|
|
|
25
25
|
requirements:
|
|
26
26
|
- - ">="
|
|
27
27
|
- !ruby/object:Gem::Version
|
|
28
|
-
version: 0.
|
|
28
|
+
version: 0.3.0
|
|
29
29
|
- - "<"
|
|
30
30
|
- !ruby/object:Gem::Version
|
|
31
31
|
version: 0.4.0
|