geosparql_to_geojson 0.1.2 → 0.1.3
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/geosparql_to_geojson/converter.rb +47 -12
- data/lib/geosparql_to_geojson/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: 8745dbc21bfac65f5e4ff1e9a195d561d9f15d5c
|
4
|
+
data.tar.gz: 3f52ce971cf31c957258a911506ff2c76f714ef8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4614f07cac499f359243bc66a76c952914995a51ca834ffd0fc5f02f5f2ea2c23c298bd18e1a52b12b2dc192572fbeec4563e763f74bf177e122d08915e8909a
|
7
|
+
data.tar.gz: 5c64b4b1db9f453052e9ed53b80bcb034df71d7097cdcf7950e9c405c113a424caff02a71bfe0b0bce66af27110fc2c2fa8e0683541ada4f6cdeb5f1e04aeb9c
|
@@ -5,8 +5,19 @@ module GeosparqlToGeojson
|
|
5
5
|
#
|
6
6
|
# @since 0.1.0
|
7
7
|
class Converter
|
8
|
-
# Constant
|
9
|
-
|
8
|
+
# Constant containing hash of GeoSparql types and the correctly formatted version.
|
9
|
+
GEOSPARQL_TYPES = {
|
10
|
+
polygon: :Polygon,
|
11
|
+
point: :Point,
|
12
|
+
multipoint: :MultiPoint,
|
13
|
+
linestring: :LineString,
|
14
|
+
multipolygon: :MultiPolygon,
|
15
|
+
geometrycollection: :GeometryCollection,
|
16
|
+
multiline: :Multiline
|
17
|
+
}.freeze
|
18
|
+
|
19
|
+
# Constant regex containing every GeoSparql data type that finds the type and the type's values.
|
20
|
+
GEOMETRY_REGEX = /(#{GEOSPARQL_TYPES.values.join('|')})\(+(.*?)\)+/i
|
10
21
|
|
11
22
|
# Creates a new instance of GeosparqlToGeojson::Converter
|
12
23
|
#
|
@@ -39,17 +50,40 @@ module GeosparqlToGeojson
|
|
39
50
|
GeosparqlToGeojson::GeoJson.new(collect_geosparql_data)
|
40
51
|
end
|
41
52
|
|
53
|
+
private
|
54
|
+
|
42
55
|
# Creates a hash of each GeoSparql type present and it's values.
|
43
56
|
def collect_geosparql_data
|
44
57
|
@data_store = {}
|
45
|
-
|
46
|
-
|
47
|
-
@
|
58
|
+
|
59
|
+
if @geosparql_values.is_a?(Array)
|
60
|
+
@geosparql_values.each do |value|
|
61
|
+
scanned_data = value.scan(GEOMETRY_REGEX)
|
62
|
+
populate_data_hash(scanned_data)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
scanned_data = @geosparql_values.scan(GEOMETRY_REGEX)
|
66
|
+
populate_data_hash(scanned_data)
|
48
67
|
end
|
49
68
|
|
50
69
|
format_geosparql_data
|
51
70
|
end
|
52
71
|
|
72
|
+
# Sets the hash key to the GeoSparql type if it isn't already set and adds the GeoSparql values
|
73
|
+
def populate_data_hash(scanned_geosparql_data)
|
74
|
+
scanned_geosparql_data.each do |data|
|
75
|
+
key = convert_key_to_correct_format(data[0])
|
76
|
+
@data_store[key] = [] unless @data_store[key]
|
77
|
+
@data_store[key] << data[1]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Converts the key that's captured by the regex into the correct format
|
82
|
+
def convert_key_to_correct_format(key)
|
83
|
+
key = key.downcase
|
84
|
+
GEOSPARQL_TYPES[key.to_sym]
|
85
|
+
end
|
86
|
+
|
53
87
|
# Splits values into arrays and converts them into floats.
|
54
88
|
def format_geosparql_data
|
55
89
|
@data_store.keys.each do |key|
|
@@ -69,7 +103,9 @@ module GeosparqlToGeojson
|
|
69
103
|
#
|
70
104
|
# @return [Array]
|
71
105
|
def format_data(values, key)
|
72
|
-
values =
|
106
|
+
values = values.first if values.is_a?(Array)
|
107
|
+
values = values.split(/[\s]|[,]/).map!(&:to_f)
|
108
|
+
values.reverse! if @reverse
|
73
109
|
|
74
110
|
values = values.each_slice(2).to_a if key != :Point
|
75
111
|
values = [values] if key != :Point && key != :LineString
|
@@ -81,7 +117,7 @@ module GeosparqlToGeojson
|
|
81
117
|
@data_hash_array = []
|
82
118
|
@data_store.keys.each do |key|
|
83
119
|
@data_store[key.to_sym].each do |data|
|
84
|
-
@data_hash_array << generate_feature_hash(
|
120
|
+
@data_hash_array << generate_feature_hash(type: key.to_s, coordinates: data)
|
85
121
|
end
|
86
122
|
end
|
87
123
|
|
@@ -103,11 +139,10 @@ module GeosparqlToGeojson
|
|
103
139
|
#
|
104
140
|
# @return [String] a string of GeoJSON
|
105
141
|
def generate_feature_collection
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
data_hash.to_json
|
142
|
+
{
|
143
|
+
type: 'FeatureCollection',
|
144
|
+
features: @data_hash_array
|
145
|
+
}.to_json
|
111
146
|
end
|
112
147
|
end
|
113
148
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geosparql_to_geojson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Callum Neve-Jones
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|