Class: GeosparqlToGeojson::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/geosparql_to_geojson/converter.rb

Overview

Class to convert GeoSparql to GeoJSON data.

Since:

  • 0.1.0

Constant Summary collapse

GEOMETRY_TYPES =

Constant contains every GeoSparql data type.

Since:

  • 0.1.0

%w[Point Multipoint LineString Multiline Polygon MultiPolygon GeometryCollection].freeze

Instance Method Summary collapse

Constructor Details

#initialize(geosparql_values, geosparql_properties, reverse) ⇒ Converter

Creates a new instance of GeosparqlToGeojson::Converter

Parameters:

  • geosparql_data (String)

    the GeoSparql data to be converted into GeoJSON.

Since:

  • 0.1.0



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_dataObject

Creates a hash of each GeoSparql type present and it’s values.

Since:

  • 0.1.0



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

#convertObject

Method calls GeosparqlToGeojson::Converter#collect_geosparql_data to start converting data.

Examples:

Converting GeoSparql string into GeoJSON

GeosparqlToGeojson::Converter.new('Point(1.23 9.87)').convert
#=> '{
       "type": "FeatureCollection",
       "features": [
         {
           "type": "Feature",
           "geometry": {
             "type": "Point",
             "coordinates": [
               1.23,
               9.87
             ]
           },
           "properties": {}
     }'

Since:

  • 0.1.0



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.

Parameters:

  • values (String)

    the GeoSparql data to be converted into GeoJSON.

  • key (Symbol)

    the type of GeoSparql data

Returns:

  • (Array)

Since:

  • 0.1.0



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_dataObject

Splits values into arrays and converts them into floats.

Since:

  • 0.1.0



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_collectionString

Adds GeoJSON ‘feature’ hash to a GeoJSON ‘FeatureCollections’ type.

Returns:

  • (String)

    a string of GeoJSON

Since:

  • 0.1.0



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.

Returns:

  • (Hash)

    a hash containing GeoSparql data

Since:

  • 0.1.0



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_valuesObject

Created a hash from the GeoSparql values in the GeoJSON ‘Feature’ format.

Since:

  • 0.1.0



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