Class: Satellite::Sentinel2::CoordinateConverterService

Inherits:
Object
  • Object
show all
Defined in:
lib/satellite/sentinel2/coordinate_converter_service.rb

Constant Summary collapse

KML_FILE =
'S2A_OPER_GIP_TILPAR_MPC__20151209T095117_V20150622T000000_21000101T000000_B00.kml'
KML_FILE_URL =
"https://sentinel.esa.int/documents/247904/1955685/#{KML_FILE}"
FILE_SIZE =
108817408

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, lon) ⇒ CoordinateConverterService

Returns a new instance of CoordinateConverterService.



15
16
17
18
# File 'lib/satellite/sentinel2/coordinate_converter_service.rb', line 15

def initialize(lat, lon)
  self.lat = lat
  self.lon = lon
end

Instance Attribute Details

#latObject

Returns the value of attribute lat.



13
14
15
# File 'lib/satellite/sentinel2/coordinate_converter_service.rb', line 13

def lat
  @lat
end

#lonObject

Returns the value of attribute lon.



13
14
15
# File 'lib/satellite/sentinel2/coordinate_converter_service.rb', line 13

def lon
  @lon
end

Instance Method Details

#convertObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/satellite/sentinel2/coordinate_converter_service.rb', line 20

def convert
  cache_key = "Sentinel2KML-#{FILE_SIZE}"
  cached = Redis.current.get(cache_key)
  if cached
    cached = JSON.parse(cached)
  else
    cached = parsed_kml
    Redis.current.set(cache_key, cached.to_json, ex: 86400)
  end 

  found = cached.find do |polygon|
    Map::PolygonService.new.inside_polygons?({latitude: lat, longitude: lon}, polygon['coordinates'])
  end

  return unless found

  creator = Map::KmlCreatorService.new
  found['coordinates'].each { |c| creator.add_polygon(c) }
  kml_service = Map::KmlService.new(creator.to_xml)
  bounds = {
    point_more_south_west: kml_service.point_more_south_west,
    point_more_north_east: kml_service.point_more_north_east
  }

  {
    name: found['name'],
    bounds: bounds
  }
end