class GeoRuby::GeoJSONParser

GeoJSON main parser

Attributes

geometry[R]

Public Instance Methods

parse(geojson, srid = DEFAULT_SRID) click to toggle source
# File lib/geo_ruby/geojson.rb, line 76
def parse(geojson, srid = DEFAULT_SRID)
  @geometry = nil
  geohash = JSON.parse(geojson)
  parse_geohash(geohash, srid)
end

Private Instance Methods

parse_geohash(geohash, srid) click to toggle source
# File lib/geo_ruby/geojson.rb, line 84
def parse_geohash(geohash, srid)
  srid = srid_from_crs(geohash['crs']) || srid
  case geohash['type']
  when 'Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon',
       'MultiPolygon', 'GeometryCollection'
    @geometry = parse_geometry(geohash, srid)
  when 'Feature'
    @geometry = parse_geojson_feature(geohash, srid)
  when 'FeatureCollection'
    @geometry = parse_geojson_feature_collection(geohash, srid)
  else
    fail GeoJSONFormatError, 'Unknown GeoJSON type'
  end
end
parse_geojson_feature(geohash, srid) click to toggle source
# File lib/geo_ruby/geojson.rb, line 115
def parse_geojson_feature(geohash, srid)
  srid = srid_from_crs(geohash['crs']) || srid
  geometry = parse_geometry(geohash['geometry'], srid)
  GeoJSONFeature.new(geometry, geohash['properties'], geohash['id'])
end
parse_geojson_feature_collection(geohash, srid) click to toggle source
# File lib/geo_ruby/geojson.rb, line 121
def parse_geojson_feature_collection(geohash, srid)
  srid = srid_from_crs(geohash['crs']) || srid
  features = []
  geohash['features'].each do |feature|
    features << parse_geojson_feature(feature, srid)
  end
  GeoJSONFeatureCollection.new(features)
end
parse_geometry(geohash, srid) click to toggle source
# File lib/geo_ruby/geojson.rb, line 99
def parse_geometry(geohash, srid)
  srid = srid_from_crs(geohash['crs']) || srid
  if geohash['type'] == 'GeometryCollection'
    parse_geometry_collection(geohash, srid)
  else
    klass = GeoRuby::SimpleFeatures.const_get(geohash['type'])
    klass.from_coordinates(geohash['coordinates'], srid, false, false)
  end
end
parse_geometry_collection(geohash, srid) click to toggle source
# File lib/geo_ruby/geojson.rb, line 109
def parse_geometry_collection(geohash, srid)
  srid = srid_from_crs(geohash['crs']) || srid
  geometries = geohash['geometries'].map { |g| parse_geometry(g, srid) }
  GeometryCollection.from_geometries(geometries, srid)
end
srid_from_crs(crs) click to toggle source
# File lib/geo_ruby/geojson.rb, line 130
def srid_from_crs(crs)
  # We somehow need to map crs to srid, currently only support for EPSG
  if crs && crs['type'] == 'OGC'
    urn = crs['properties']['urn'].split(':')
    return urn.last if urn[4] == 'EPSG'
  end
  nil
end