Class: GeosparqlToGeojson::Converter
- Inherits:
-
Object
- Object
- GeosparqlToGeojson::Converter
- Defined in:
- lib/geosparql_to_geojson/converter.rb
Overview
Class to convert GeoSparql to GeoJSON data.
Constant Summary collapse
- GEOMETRY_TYPES =
Constant contains every GeoSparql data type.
%w[Point Multipoint LineString Multiline Polygon MultiPolygon GeometryCollection].freeze
Instance Method Summary collapse
-
#collect_geosparql_data ⇒ Object
Creates a hash of each GeoSparql type present and it’s values.
-
#convert ⇒ Object
Method calls GeosparqlToGeojson::Converter#collect_geosparql_data to start converting data.
-
#format_data(values, key) ⇒ Array
Formats GeoSparql data.
-
#format_geosparql_data ⇒ Object
Splits values into arrays and converts them into floats.
-
#generate_feature_collection ⇒ String
Adds GeoJSON ‘feature’ hash to a GeoJSON ‘FeatureCollections’ type.
-
#generate_feature_hash(data_hash) ⇒ Hash
Adds converted GeoSparql data to a GeoJSON ‘feature’ type.
-
#generate_hash_from_values ⇒ Object
Created a hash from the GeoSparql values in the GeoJSON ‘Feature’ format.
-
#initialize(geosparql_values, geosparql_properties, reverse) ⇒ Converter
constructor
Creates a new instance of GeosparqlToGeojson::Converter.
Constructor Details
#initialize(geosparql_values, geosparql_properties, reverse) ⇒ Converter
Creates a new instance of GeosparqlToGeojson::Converter
14 15 16 17 18 |
# File 'lib/geosparql_to_geojson/converter.rb', line 14 def initialize(geosparql_values, geosparql_properties, reverse) @geosparql_values = geosparql_values @geosparql_properties = geosparql_properties @reverse = reverse end |
Instance Method Details
#collect_geosparql_data ⇒ Object
Creates a hash of each GeoSparql type present and it’s values.
43 44 45 46 47 48 49 50 51 |
# File 'lib/geosparql_to_geojson/converter.rb', line 43 def collect_geosparql_data @data_store = {} GEOMETRY_TYPES.each do |geometry_type| geosparql_data_match = @geosparql_values.scan(/#{geometry_type}\(+(.*?)\)+/i) @data_store[geometry_type.to_sym] = geosparql_data_match unless geosparql_data_match.empty? end format_geosparql_data end |
#convert ⇒ Object
Method calls GeosparqlToGeojson::Converter#collect_geosparql_data to start converting data.
38 39 40 |
# File 'lib/geosparql_to_geojson/converter.rb', line 38 def convert GeosparqlToGeojson::GeoJson.new(collect_geosparql_data) end |
#format_data(values, key) ⇒ Array
Formats GeoSparql data. Will reverse the values if @reverse is set to true.
71 72 73 74 75 76 77 |
# File 'lib/geosparql_to_geojson/converter.rb', line 71 def format_data(values, key) values = @reverse ? values[0].split(/[\s]|[,]/).map!(&:to_f).reverse : values[0].split(/[\s]|[,]/).map!(&:to_f) values = values.each_slice(2).to_a if key != :Point values = [values] if key != :Point && key != :LineString values end |
#format_geosparql_data ⇒ Object
Splits values into arrays and converts them into floats.
54 55 56 57 58 59 60 61 62 |
# File 'lib/geosparql_to_geojson/converter.rb', line 54 def format_geosparql_data @data_store.keys.each do |key| @data_store[key.to_sym].map! do |values| format_data(values, key) end end generate_hash_from_values end |
#generate_feature_collection ⇒ String
Adds GeoJSON ‘feature’ hash to a GeoJSON ‘FeatureCollections’ type.
105 106 107 108 109 110 111 |
# File 'lib/geosparql_to_geojson/converter.rb', line 105 def generate_feature_collection data_hash = { type: 'FeatureCollection', features: @data_hash_array } data_hash.to_json end |
#generate_feature_hash(data_hash) ⇒ Hash
Adds converted GeoSparql data to a GeoJSON ‘feature’ type.
94 95 96 97 98 99 100 |
# File 'lib/geosparql_to_geojson/converter.rb', line 94 def generate_feature_hash(data_hash) { type: 'Feature', geometry: data_hash, properties: @geosparql_properties } end |
#generate_hash_from_values ⇒ Object
Created a hash from the GeoSparql values in the GeoJSON ‘Feature’ format.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/geosparql_to_geojson/converter.rb', line 80 def generate_hash_from_values @data_hash_array = [] @data_store.keys.each do |key| @data_store[key.to_sym].each do |data| @data_hash_array << generate_feature_hash({ type: key.to_s, coordinates: data }) end end generate_feature_collection end |