Class: Sentinel2Aws::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sentinel2_aws/client.rb

Constant Summary collapse

S3_REGION =
'eu-central-1'
S3_BUCKET =
'sentinel-s2-l1c'

Instance Method Summary collapse

Constructor Details

#initialize(akid, secret) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
# File 'lib/sentinel2_aws/client.rb', line 6

def initialize(akid, secret)
  Aws.config.update({
    credentials: Aws::Credentials.new(akid, secret),
    region: S3_REGION
  })
  @s3_resource = Aws::S3::Resource.new
  @s3_client = Aws::S3::Client.new
  @directory = Dir.mktmpdir
end

Instance Method Details

#download_file(s3_file, folder, filename) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sentinel2_aws/client.rb', line 99

def download_file(s3_file, folder, filename)
  if @s3_resource.bucket(S3_BUCKET).object(s3_file).exists?
    base_dir = "#{@directory}/#{folder}"
    Dir.mkdir(base_dir) unless File.exists?(base_dir)
    filepath = "#{base_dir}/#{filename}"
    obj = @s3_resource.bucket(S3_BUCKET).object(s3_file)
    obj.get(response_target: filepath)
    return filepath
  else
    return nil
  end
end

#download_tile_data(tile_path, filename, output_path) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sentinel2_aws/client.rb', line 112

def download_tile_data(tile_path, filename, output_path)
  s3_file = "#{tile_path}/#{filename}"
  if @s3_resource.bucket(S3_BUCKET).object(s3_file).exists?
    filepath = "#{output_path}/#{filename}"
    obj = @s3_resource.bucket(S3_BUCKET).object(s3_file)
    obj.get(response_target: filepath)
    return filepath
  else
    return nil
  end
end

#get_product_info(product) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sentinel2_aws/client.rb', line 51

def get_product_info(product)
  product_str = product.split('/')
  folder = "#{product_str[1]}_#{product_str[2]}_#{product_str[3]}"
  filepath = download_file(product, folder, "#{product_str[4]}.json")
  unless filepath.nil?
    file = File.read(filepath)
    product_info = Oj.load(file)
    FileUtils.remove_entry(filepath)
    tiles = []
    product_info["tiles"].each do |tile|
      tile_path = tile["path"]
      tile_name = tile_path.gsub("/", "_")
      tile = "#{tile["path"]}/tileInfo.json"
      tile_filepath = download_file(tile, folder, "#{tile_name}.json")
      unless tile_filepath.nil?
        tile_file = File.read(tile_filepath)
        tile_info = Oj.load(tile_file)
        FileUtils.remove_entry(tile_filepath)

        epsg_str = tile_info["tileGeometry"]["crs"]["properties"]["name"].split(':')

         = {
          path: tile_info["path"],
          timestamp: tile_info["timestamp"],
          utm_zone: tile_info["utmZone"],
          latitude_band: tile_info["latitudeBand"],
          grid_square: tile_info["gridSquare"],
          epsg: epsg_str[-1],
          data_coverage_percentage: tile_info["dataCoveragePercentage"],
          cloudy_pixel_percentage: tile_info["cloudyPixelPercentage"]
        }
        tiles << 
      end
    end

     = {
      name: product_info["name"],
      id: product_info["id"],
      path: product_info["path"],
      timestamp: product_info["timestamp"],
      datatake_identifier: product_info["datatakeIdentifier"],
      scihub_ingestion: product_info["sciHubIngestion"],
      s3_ingestion: product_info["s3Ingestion"],
      tiles: tiles
    }
  end
end

#get_products(date) ⇒ Object

date format: “YYYY-MM-DD”



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sentinel2_aws/client.rb', line 27

def get_products(date)
  pdate = parse_date(date)
  unless pdate.nil?
    year, month, day = pdate
    prefix = "products/#{year}/#{month}/#{day}/"
    folder = "#{year}_#{month}_#{day}"
    objects = []

    @s3_client.list_objects(bucket: S3_BUCKET, prefix: prefix).each do |response|
      response.contents.each do |object|
        product_str = object.key.split(prefix)
        product = product_str[1].split('/')
        if product[1] == 'productInfo.json'
          objects << object.key
        end
      end
    end

    return objects
  else
    return []
  end
end

#parse_date(date) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/sentinel2_aws/client.rb', line 16

def parse_date(date)
  y, m, d = date.split('-')
  y, m, d = y.to_i, m.to_i, d.to_i
  if Date.valid_date? y, m, d
    return [y, m, d]
  else
    return nil
  end
end

#remove_directoryObject



124
125
126
# File 'lib/sentinel2_aws/client.rb', line 124

def remove_directory
  FileUtils.remove_dir(@directory)
end