geosparql_to_geojson 0.1.3 → 0.2.0
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/gocd/web1live.yaml +30 -0
- data/lib/geosparql_to_geojson.rb +3 -1
- data/lib/geosparql_to_geojson/converter.rb +10 -142
- data/lib/geosparql_to_geojson/converter/base_converter.rb +187 -0
- data/lib/geosparql_to_geojson/converter/polygon_converter.rb +204 -0
- data/lib/geosparql_to_geojson/geojson.rb +4 -0
- data/lib/geosparql_to_geojson/geojson_validator.rb +8 -3
- data/lib/geosparql_to_geojson/version.rb +6 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ade4cd28437367ccc541abe72a5e55b689768a47
|
4
|
+
data.tar.gz: 15ea869f499f0e99191d615c5187716705fc3861
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2dc79686b00b5e1868be0fe1a0972459901675a1cf66d248f18672eaad5e548c782a3f945f28054e516a91410836d59a59f6971ca8ac6e73cade017dd7e3376
|
7
|
+
data.tar.gz: 791611cb8d76ad9b7971dfc6947735e70283537c80c48be7857fdbd2bf81a953a14b1390356fcec98da6f134f27525e4bb3538404d3d32077742a17f448ba127
|
data/gocd/web1live.yaml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
pipelines:
|
2
|
+
GEM-geosparql-to-geojson:
|
3
|
+
group: Dependencies
|
4
|
+
label_template: "${COUNT}"
|
5
|
+
materials:
|
6
|
+
GEM-geosparql-to-geojson-git:
|
7
|
+
git: https://github.com/ukparliament/geosparql_to_geojson.git
|
8
|
+
branch: master
|
9
|
+
auto_update: true
|
10
|
+
stages:
|
11
|
+
- deploy:
|
12
|
+
approval: manual
|
13
|
+
jobs:
|
14
|
+
deploy:
|
15
|
+
tasks:
|
16
|
+
- exec:
|
17
|
+
run_if: passed
|
18
|
+
command: make
|
19
|
+
arguments:
|
20
|
+
- checkout_to_release
|
21
|
+
- exec:
|
22
|
+
run_if: passed
|
23
|
+
command: make
|
24
|
+
arguments:
|
25
|
+
- deploy_to_release
|
26
|
+
|
27
|
+
environments:
|
28
|
+
Web.LIVE:
|
29
|
+
pipelines:
|
30
|
+
- GEM-geosparql-to-geojson
|
data/lib/geosparql_to_geojson.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'geosparql_to_geojson/version'
|
2
4
|
require 'geosparql_to_geojson/converter'
|
3
5
|
require 'geosparql_to_geojson/geojson'
|
@@ -14,6 +16,6 @@ module GeosparqlToGeojson
|
|
14
16
|
#
|
15
17
|
# @return [GeosparqlToGeojson::GeoJson]
|
16
18
|
def self.convert_to_geojson(geosparql_values: '', geosparql_properties: {}, reverse: false)
|
17
|
-
GeosparqlToGeojson::Converter.new(geosparql_values, geosparql_properties, reverse).convert
|
19
|
+
GeosparqlToGeojson::Converter::BaseConverter.new(geosparql_values, geosparql_properties, reverse).convert
|
18
20
|
end
|
19
21
|
end
|
@@ -1,148 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'json'
|
2
4
|
|
5
|
+
# Namespace for classes and modules that deal with converting GeoSparql to GeoJSON
|
6
|
+
#
|
7
|
+
# @since 0.1.0
|
3
8
|
module GeosparqlToGeojson
|
4
|
-
#
|
9
|
+
# Namespace for classes that convert GeoSparql to GeoJSON data.
|
5
10
|
#
|
6
|
-
# @since 0.
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
21
|
-
|
22
|
-
# Creates a new instance of GeosparqlToGeojson::Converter
|
23
|
-
#
|
24
|
-
# @param [String] geosparql_data the GeoSparql data to be converted into GeoJSON.
|
25
|
-
def initialize(geosparql_values, geosparql_properties, reverse)
|
26
|
-
@geosparql_values = geosparql_values
|
27
|
-
@geosparql_properties = geosparql_properties
|
28
|
-
@reverse = reverse
|
29
|
-
end
|
30
|
-
|
31
|
-
# Method calls GeosparqlToGeojson::Converter#collect_geosparql_data to start converting data.
|
32
|
-
#
|
33
|
-
# @example Converting GeoSparql string into GeoJSON
|
34
|
-
# GeosparqlToGeojson::Converter.new('Point(1.23 9.87)').convert
|
35
|
-
# #=> '{
|
36
|
-
# "type": "FeatureCollection",
|
37
|
-
# "features": [
|
38
|
-
# {
|
39
|
-
# "type": "Feature",
|
40
|
-
# "geometry": {
|
41
|
-
# "type": "Point",
|
42
|
-
# "coordinates": [
|
43
|
-
# 1.23,
|
44
|
-
# 9.87
|
45
|
-
# ]
|
46
|
-
# },
|
47
|
-
# "properties": {}
|
48
|
-
# }'
|
49
|
-
def convert
|
50
|
-
GeosparqlToGeojson::GeoJson.new(collect_geosparql_data)
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
# Creates a hash of each GeoSparql type present and it's values.
|
56
|
-
def collect_geosparql_data
|
57
|
-
@data_store = {}
|
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)
|
67
|
-
end
|
68
|
-
|
69
|
-
format_geosparql_data
|
70
|
-
end
|
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
|
-
|
87
|
-
# Splits values into arrays and converts them into floats.
|
88
|
-
def format_geosparql_data
|
89
|
-
@data_store.keys.each do |key|
|
90
|
-
@data_store[key.to_sym].map! do |values|
|
91
|
-
format_data(values, key)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
generate_hash_from_values
|
96
|
-
end
|
97
|
-
|
98
|
-
# Formats GeoSparql data.
|
99
|
-
# Will reverse the values if @reverse is set to true.
|
100
|
-
#
|
101
|
-
# @param [String] values the GeoSparql data to be converted into GeoJSON.
|
102
|
-
# @param [Symbol] key the type of GeoSparql data
|
103
|
-
#
|
104
|
-
# @return [Array]
|
105
|
-
def format_data(values, key)
|
106
|
-
values = values.first if values.is_a?(Array)
|
107
|
-
values = values.split(/[\s]|[,]/).map!(&:to_f)
|
108
|
-
values.reverse! if @reverse
|
109
|
-
|
110
|
-
values = values.each_slice(2).to_a if key != :Point
|
111
|
-
values = [values] if key != :Point && key != :LineString
|
112
|
-
values
|
113
|
-
end
|
114
|
-
|
115
|
-
# Created a hash from the GeoSparql values in the GeoJSON 'Feature' format.
|
116
|
-
def generate_hash_from_values
|
117
|
-
@data_hash_array = []
|
118
|
-
@data_store.keys.each do |key|
|
119
|
-
@data_store[key.to_sym].each do |data|
|
120
|
-
@data_hash_array << generate_feature_hash(type: key.to_s, coordinates: data)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
generate_feature_collection
|
125
|
-
end
|
126
|
-
|
127
|
-
# Adds converted GeoSparql data to a GeoJSON 'feature' type.
|
128
|
-
#
|
129
|
-
# @return [Hash] a hash containing GeoSparql data
|
130
|
-
def generate_feature_hash(data_hash)
|
131
|
-
{
|
132
|
-
type: 'Feature',
|
133
|
-
geometry: data_hash,
|
134
|
-
properties: @geosparql_properties
|
135
|
-
}
|
136
|
-
end
|
137
|
-
|
138
|
-
# Adds GeoJSON 'feature' hash to a GeoJSON 'FeatureCollections' type.
|
139
|
-
#
|
140
|
-
# @return [String] a string of GeoJSON
|
141
|
-
def generate_feature_collection
|
142
|
-
{
|
143
|
-
type: 'FeatureCollection',
|
144
|
-
features: @data_hash_array
|
145
|
-
}.to_json
|
146
|
-
end
|
11
|
+
# @since 0.2.0
|
12
|
+
module Converter
|
13
|
+
require 'geosparql_to_geojson/converter/base_converter'
|
14
|
+
require 'geosparql_to_geojson/converter/polygon_converter'
|
147
15
|
end
|
148
16
|
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Namespace for classes and modules that deal with converting GeoSparql to GeoJSON
|
4
|
+
#
|
5
|
+
# @since 0.1.0
|
6
|
+
module GeosparqlToGeojson
|
7
|
+
# Namespace for classes that convert GeoSparql to GeoJSON data.
|
8
|
+
#
|
9
|
+
# @since 0.2.0
|
10
|
+
module Converter
|
11
|
+
# Class to convert GeoSparql to GeoJSON data.
|
12
|
+
#
|
13
|
+
# @since 0.1.0
|
14
|
+
# @attr_reader [String] geosparql_values the GeoSPARQL values to be converted.
|
15
|
+
# @attr_reader [Hash] geosparql_properties the GeoJSON properties to be added to the converted GeoSPARQL.
|
16
|
+
# @attr_reader [Boolean] reverse whether the polygon data needs to be reversed.
|
17
|
+
class BaseConverter
|
18
|
+
attr_reader :geosparql_values, :geosparql_properties, :reverse
|
19
|
+
|
20
|
+
# Constant containing hash of GeoSparql types and the correctly formatted version.
|
21
|
+
GEOSPARQL_TYPES = {
|
22
|
+
polygon: :Polygon,
|
23
|
+
point: :Point,
|
24
|
+
multipoint: :MultiPoint,
|
25
|
+
linestring: :LineString,
|
26
|
+
multipolygon: :MultiPolygon,
|
27
|
+
geometrycollection: :GeometryCollection,
|
28
|
+
multiline: :Multiline
|
29
|
+
}.freeze
|
30
|
+
|
31
|
+
# Constant regex containing every GeoSparql data type that finds the type and the type's values.
|
32
|
+
GEOMETRY_REGEX = /(#{GEOSPARQL_TYPES.values.join('|')})?\(+(.*?)\)+/i
|
33
|
+
|
34
|
+
# Creates a new instance of GeosparqlToGeojson::Converter::BaseConverter
|
35
|
+
#
|
36
|
+
# @param [String] geosparql_values the GeoSparql data to be converted into GeoJSON.
|
37
|
+
# @param [Hash] geosparql_properties the properties to be added to the formatted GeoJSON.
|
38
|
+
# @param [Boolean] reverse the geosparql_values data.
|
39
|
+
def initialize(geosparql_values, geosparql_properties, reverse)
|
40
|
+
@geosparql_values = geosparql_values
|
41
|
+
@geosparql_properties = geosparql_properties
|
42
|
+
@reverse = reverse
|
43
|
+
end
|
44
|
+
|
45
|
+
# Method calls GeosparqlToGeojson::Converter#collect_geosparql_data to start converting data.
|
46
|
+
#
|
47
|
+
# @example Converting GeoSparql string into GeoJSON
|
48
|
+
# GeosparqlToGeojson::Converter.new('Point(1.23 9.87)').convert
|
49
|
+
# #=> '{
|
50
|
+
# "type": "FeatureCollection",
|
51
|
+
# "features": [
|
52
|
+
# {
|
53
|
+
# "type": "Feature",
|
54
|
+
# "geometry": {
|
55
|
+
# "type": "Point",
|
56
|
+
# "coordinates": [
|
57
|
+
# 1.23,
|
58
|
+
# 9.87
|
59
|
+
# ]
|
60
|
+
# },
|
61
|
+
# "properties": {}
|
62
|
+
# }'
|
63
|
+
def convert
|
64
|
+
GeosparqlToGeojson::GeoJson.new(collect_geosparql_data)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
# Creates a hash of each GeoSparql type present and it's values.
|
70
|
+
#
|
71
|
+
# @return [void]
|
72
|
+
def collect_geosparql_data
|
73
|
+
@geosparql_values_by_type = {}
|
74
|
+
if geosparql_values.is_a?(Array)
|
75
|
+
geosparql_values.each do |value|
|
76
|
+
scanned_data = value.scan(GEOMETRY_REGEX)
|
77
|
+
populate_data_hash(scanned_data)
|
78
|
+
end
|
79
|
+
else
|
80
|
+
scanned_data = geosparql_values.scan(GEOMETRY_REGEX)
|
81
|
+
populate_data_hash(scanned_data)
|
82
|
+
end
|
83
|
+
|
84
|
+
format_geosparql_data
|
85
|
+
end
|
86
|
+
|
87
|
+
# Sets the hash key to the GeoSparql type if it isn't already set and add the GeoSparql values
|
88
|
+
#
|
89
|
+
# @param [Array<String>] scanned_geosparql_data the GeoSPARQL data to convert.
|
90
|
+
# @return [void]
|
91
|
+
def populate_data_hash(scanned_geosparql_data)
|
92
|
+
scanned_geosparql_data.each_with_index do |data, index|
|
93
|
+
# If the data is missing a key we use the previous data points key or fallback to using 'polygon'
|
94
|
+
data[0].nil? ? data[0] = scanned_geosparql_data[index - 1][0] : 'polygon'
|
95
|
+
|
96
|
+
key = convert_key_to_correct_format(data[0])
|
97
|
+
@geosparql_values_by_type[key] = [] unless @geosparql_values_by_type[key]
|
98
|
+
@geosparql_values_by_type[key] << data[1]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Converts the key that's captured by the regex into the correct format.
|
103
|
+
#
|
104
|
+
# @param [String] key the GeoJSON type.
|
105
|
+
# @return [String] the formatted GeoJSON version of the key.
|
106
|
+
def convert_key_to_correct_format(key)
|
107
|
+
key = key.downcase
|
108
|
+
GEOSPARQL_TYPES[key.to_sym]
|
109
|
+
end
|
110
|
+
|
111
|
+
# Splits values into arrays and converts them into floats.
|
112
|
+
# Also calls PolygonConverter to convert any polygon data that might be present.
|
113
|
+
#
|
114
|
+
# @return [void]
|
115
|
+
def format_geosparql_data
|
116
|
+
polygons = []
|
117
|
+
@geosparql_values_by_type.each_key do |key|
|
118
|
+
if key == :Polygon
|
119
|
+
polygons << @geosparql_values_by_type[key]
|
120
|
+
else
|
121
|
+
@geosparql_values_by_type[key].map! do |values|
|
122
|
+
format_data(values, key)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Updates @geosparql_values_by_type with formatted polygon data (including holes).
|
128
|
+
GeosparqlToGeojson::Converter::PolygonConverter.new(@geosparql_values_by_type, polygons).convert if polygons.any?
|
129
|
+
|
130
|
+
generate_hash_from_values
|
131
|
+
end
|
132
|
+
|
133
|
+
# Formats GeoSparql data.
|
134
|
+
# Will reverse the values if @reverse is set to true.
|
135
|
+
#
|
136
|
+
# @param [String] values the GeoSparql data to be converted into GeoJSON.
|
137
|
+
# @param [Symbol] key the type of GeoSparql data.
|
138
|
+
#
|
139
|
+
# @return [Array]
|
140
|
+
def format_data(values, key)
|
141
|
+
values = values.first if values.is_a?(Array)
|
142
|
+
values = values.split(/[\s,]+/).map!(&:to_f)
|
143
|
+
values.reverse! if reverse
|
144
|
+
|
145
|
+
values = values.each_slice(2).to_a if key != :Point
|
146
|
+
values = [values] if key != :Point && key != :LineString
|
147
|
+
values
|
148
|
+
end
|
149
|
+
|
150
|
+
# Created a hash from the GeoSparql values in the GeoJSON 'Feature' format.
|
151
|
+
#
|
152
|
+
# @return [void]
|
153
|
+
def generate_hash_from_values
|
154
|
+
@data_hash_array = []
|
155
|
+
@geosparql_values_by_type.each_key do |key|
|
156
|
+
@geosparql_values_by_type[key].each do |data|
|
157
|
+
@data_hash_array << generate_feature_hash(type: key.to_s, coordinates: data)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
generate_feature_collection
|
162
|
+
end
|
163
|
+
|
164
|
+
# Adds converted GeoSparql data to a GeoJSON 'Feature' type.
|
165
|
+
#
|
166
|
+
# @param [Hash] data_hash the formatted GeoJSON data.
|
167
|
+
# @return [Hash] a hash containing GeoJSON data.
|
168
|
+
def generate_feature_hash(data_hash)
|
169
|
+
{
|
170
|
+
type: 'Feature',
|
171
|
+
geometry: data_hash,
|
172
|
+
properties: geosparql_properties
|
173
|
+
}
|
174
|
+
end
|
175
|
+
|
176
|
+
# Adds GeoJSON 'feature' hash to a GeoJSON 'FeatureCollections' type.
|
177
|
+
#
|
178
|
+
# @return [String] a string of formatted GeoJSON
|
179
|
+
def generate_feature_collection
|
180
|
+
{
|
181
|
+
type: 'FeatureCollection',
|
182
|
+
features: @data_hash_array
|
183
|
+
}.to_json
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
@@ -0,0 +1,204 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Namespace for classes and modules that deal with converting GeoSparql to GeoJSON
|
4
|
+
#
|
5
|
+
# @since 0.1.0
|
6
|
+
module GeosparqlToGeojson
|
7
|
+
# Namespace for classes that convert GeoSparql to GeoJSON data.
|
8
|
+
#
|
9
|
+
# @since 0.2.0
|
10
|
+
module Converter
|
11
|
+
# Class to convert polygon data and process polygon holes.
|
12
|
+
#
|
13
|
+
# @since 0.2.0
|
14
|
+
class PolygonConverter
|
15
|
+
# Creates a new instance of GeosparqlToGeojson::Converter::PolygonConverter
|
16
|
+
#
|
17
|
+
# @param [Hash] geosparql_values_by_type that contains the formatted GeoSPARQL data.
|
18
|
+
# @param [Array<String>] values the raw polygon data.
|
19
|
+
def initialize(geosparql_values_by_type, values)
|
20
|
+
@geosparql_values_by_type = geosparql_values_by_type
|
21
|
+
@values = values.flatten
|
22
|
+
@polygons = []
|
23
|
+
@holes = []
|
24
|
+
@formatted_polygon_array = []
|
25
|
+
end
|
26
|
+
|
27
|
+
# Converts polygon data into the correct format and adds it to @geosparql_values_by_type.
|
28
|
+
#
|
29
|
+
# @return [Hash] the converted andf formatted polygon data.
|
30
|
+
def convert
|
31
|
+
format_data
|
32
|
+
|
33
|
+
if @holes.any?
|
34
|
+
match_holes_to_polygons
|
35
|
+
format_polygons_with_holes
|
36
|
+
end
|
37
|
+
|
38
|
+
format_polygons_without_holes
|
39
|
+
add_formatted_polygons_to_data_hash
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Splits polygon value strings by GeoSparql syntax, strips whitespace and converts values to floats.
|
45
|
+
#
|
46
|
+
# @return [void]
|
47
|
+
def format_data
|
48
|
+
@values.map! do |values_string|
|
49
|
+
values_string.split(/\),?\s*\(/).map! do |values|
|
50
|
+
values = values.split(',').map(&:strip)
|
51
|
+
values.map! { |value| value.split(/\s/).map(&:to_f) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
split_into_polygons_and_holes
|
56
|
+
end
|
57
|
+
|
58
|
+
# Adds polygons and holes from @values to @polygons and @holes.
|
59
|
+
#
|
60
|
+
# @return [Array] @holes
|
61
|
+
# @return [Array] @polygons
|
62
|
+
def split_into_polygons_and_holes
|
63
|
+
@values.each do |value_array|
|
64
|
+
value_array.each do |values|
|
65
|
+
if hole?(values)
|
66
|
+
@holes << values
|
67
|
+
else
|
68
|
+
@polygons << values
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Checks whether a polygon is a hole.
|
75
|
+
# Uses the shoelace formula.
|
76
|
+
#
|
77
|
+
# @param [Array] values the polygon values.
|
78
|
+
# @return [Boolean]
|
79
|
+
def hole?(values)
|
80
|
+
results = []
|
81
|
+
count = values.size
|
82
|
+
values.each_with_index do |_value, index|
|
83
|
+
current = values[index]
|
84
|
+
next_value = values[index + 1]
|
85
|
+
|
86
|
+
if index < count - 1
|
87
|
+
results << (next_value[0] - current[0]) * (next_value[1] + current[1])
|
88
|
+
end
|
89
|
+
end
|
90
|
+
results.compact!
|
91
|
+
return true if results.sum.negative?
|
92
|
+
false
|
93
|
+
end
|
94
|
+
|
95
|
+
# Generates a hash containing the indexes of polygons and the holes within them.
|
96
|
+
#
|
97
|
+
# @return [void]
|
98
|
+
def match_holes_to_polygons
|
99
|
+
@matches = {}
|
100
|
+
@polygons.each_with_index do |polygon, polygon_index|
|
101
|
+
set_min_and_max_axis(polygon)
|
102
|
+
|
103
|
+
@holes.each_with_index do |hole, hole_index|
|
104
|
+
set_min_and_max_axis(hole)
|
105
|
+
|
106
|
+
# @note This method of building a border box around the polygon and checking
|
107
|
+
# to see whether the min and max values of the hole fit within it
|
108
|
+
# will work since we already know it is a hole. Otherwise we would need a more accurate solution.
|
109
|
+
if @x_min[1] > @x_min[0] && @x_max[1] < @x_max[0] && @y_min[1] > @y_min[0] && @y_max[1] < @y_max[0]
|
110
|
+
@matches[polygon_index] ||= []
|
111
|
+
@matches[polygon_index].push(hole_index)
|
112
|
+
end
|
113
|
+
|
114
|
+
set_min_and_max_to_first_value
|
115
|
+
end
|
116
|
+
|
117
|
+
reset_min_and_max_instance_variables
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Sets the minumum and maximum values of a polygon's X and Y axis.
|
122
|
+
#
|
123
|
+
# @param [Array<Float>] values the x and y values for each point on the polygon.
|
124
|
+
# @return [void]
|
125
|
+
def set_min_and_max_axis(values)
|
126
|
+
x_axis = []
|
127
|
+
y_axis = []
|
128
|
+
@x_min ||= []
|
129
|
+
@x_max ||= []
|
130
|
+
@y_min ||= []
|
131
|
+
@y_max ||= []
|
132
|
+
|
133
|
+
values.each do |value|
|
134
|
+
x_axis << value[0]
|
135
|
+
y_axis << value[1]
|
136
|
+
end
|
137
|
+
|
138
|
+
@x_min << x_axis.min
|
139
|
+
@x_max << x_axis.max
|
140
|
+
@y_min << y_axis.min
|
141
|
+
@y_max << y_axis.max
|
142
|
+
end
|
143
|
+
|
144
|
+
# Sets minimum and maximum X and Y axis variables to an array containing their first value.
|
145
|
+
#
|
146
|
+
# @return [void]
|
147
|
+
def set_min_and_max_to_first_value
|
148
|
+
@x_min = [@x_min.first]
|
149
|
+
@x_max = [@x_max.first]
|
150
|
+
@y_min = [@y_min.first]
|
151
|
+
@y_max = [@y_max.first]
|
152
|
+
end
|
153
|
+
|
154
|
+
# Sets minimum and maximum X and Y axis variables to nil.
|
155
|
+
#
|
156
|
+
# @return [void]
|
157
|
+
def reset_min_and_max_instance_variables
|
158
|
+
@x_min = nil
|
159
|
+
@y_min = nil
|
160
|
+
@x_max = nil
|
161
|
+
@y_max = nil
|
162
|
+
end
|
163
|
+
|
164
|
+
# Adds an array of a polygon and the holes that match the polygon to @formatted_polygon_array.
|
165
|
+
#
|
166
|
+
# @return [void]
|
167
|
+
def format_polygons_with_holes
|
168
|
+
@matches.each do |key, values|
|
169
|
+
polygon_with_holes = [@polygons[key]]
|
170
|
+
|
171
|
+
values.each do |value|
|
172
|
+
polygon_with_holes << @holes[value]
|
173
|
+
end
|
174
|
+
|
175
|
+
@formatted_polygon_array << polygon_with_holes
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
# Adds polygons that don't have holes to @formatted_polygon_array.
|
180
|
+
#
|
181
|
+
# @return [void]
|
182
|
+
def format_polygons_without_holes
|
183
|
+
remove_polygons_with_holes if @matches
|
184
|
+
|
185
|
+
@polygons.each { |polygon| @formatted_polygon_array << [polygon] }
|
186
|
+
end
|
187
|
+
|
188
|
+
# Deletes polygons that contain holes from @polygons.
|
189
|
+
#
|
190
|
+
# @return [void]
|
191
|
+
def remove_polygons_with_holes
|
192
|
+
# Need to reverse the keys so that the @polygon index doesn't change before we call #delete_at.
|
193
|
+
@matches.keys.reverse.each { |key| @polygons.delete_at(key) }
|
194
|
+
end
|
195
|
+
|
196
|
+
# Adds the populated @formatted_polygon_array to @geosparql_values_by_type
|
197
|
+
#
|
198
|
+
# @return [Hash] @geosparql_values_by_type the 'Polygon' type with it's values.
|
199
|
+
def add_formatted_polygons_to_data_hash
|
200
|
+
@geosparql_values_by_type[:Polygon] = @formatted_polygon_array
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
@@ -1,10 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'json-schema'
|
2
4
|
|
5
|
+
# Namespace for classes and modules that deal with converting GeoSparql to GeoJSON
|
6
|
+
#
|
7
|
+
# @since 0.1.0
|
3
8
|
module GeosparqlToGeojson
|
4
9
|
# Class used to validate GeoJSON
|
5
10
|
# @since 0.1.0
|
6
11
|
class GeojsonValidator
|
7
|
-
# Creates a new instance of GeosparqlToGeojson::GeojsonValidator
|
12
|
+
# Creates a new instance of GeosparqlToGeojson::GeojsonValidator.
|
8
13
|
#
|
9
14
|
# @param [String] geojson the GeoJSON data to be validated
|
10
15
|
def initialize(geojson)
|
@@ -12,14 +17,14 @@ module GeosparqlToGeojson
|
|
12
17
|
@schema = JSON.parse(File.read(File.expand_path('../schema/geojson.json', __FILE__)))
|
13
18
|
end
|
14
19
|
|
15
|
-
# Validates GeoJSON data based on JSON and GroJSON schemas
|
20
|
+
# Validates GeoJSON data based on JSON and GroJSON schemas.
|
16
21
|
#
|
17
22
|
# @return [Array] any errors with the JSON
|
18
23
|
def errors
|
19
24
|
JSON::Validator.fully_validate(@schema, @geojson)
|
20
25
|
end
|
21
26
|
|
22
|
-
# Checks whether there are any errors returned by the validator
|
27
|
+
# Checks whether there are any errors returned by the validator.
|
23
28
|
#
|
24
29
|
# @return [true, false]
|
25
30
|
def valid?
|
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.
|
4
|
+
version: 0.2.0
|
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-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -100,8 +100,11 @@ files:
|
|
100
100
|
- bin/console
|
101
101
|
- bin/setup
|
102
102
|
- geosparql_to_geojson.gemspec
|
103
|
+
- gocd/web1live.yaml
|
103
104
|
- lib/geosparql_to_geojson.rb
|
104
105
|
- lib/geosparql_to_geojson/converter.rb
|
106
|
+
- lib/geosparql_to_geojson/converter/base_converter.rb
|
107
|
+
- lib/geosparql_to_geojson/converter/polygon_converter.rb
|
105
108
|
- lib/geosparql_to_geojson/geojson.rb
|
106
109
|
- lib/geosparql_to_geojson/geojson_validator.rb
|
107
110
|
- lib/geosparql_to_geojson/schema/geojson.json
|