aletrn commited on
Commit
4e35839
1 Parent(s): caf10ab

[test] using local tile http server in lambda_handler "real" test case

Browse files
src/io/tms2geotiff.py CHANGED
@@ -137,6 +137,7 @@ def get_tile(url):
137
  retry = 3
138
  while 1:
139
  try:
 
140
  r = SESSION.get(url, timeout=60)
141
  break
142
  except Exception as request_tile_exception:
@@ -144,9 +145,7 @@ def get_tile(url):
144
  retry -= 1
145
  if not retry:
146
  raise
147
- if r.status_code == 404:
148
- return None
149
- elif not r.content:
150
  return None
151
  r.raise_for_status()
152
  return r.content
@@ -267,7 +266,7 @@ def download_extent(
267
  bigim = None
268
  base_size = [256, 256]
269
  while futures:
270
- done, not_done = concurrent.futures.wait(
271
  futures.keys(), timeout=callback_interval,
272
  return_when=concurrent.futures.FIRST_COMPLETED
273
  )
 
137
  retry = 3
138
  while 1:
139
  try:
140
+ app_logger.info(f"image tile url to download: {url}.")
141
  r = SESSION.get(url, timeout=60)
142
  break
143
  except Exception as request_tile_exception:
 
145
  retry -= 1
146
  if not retry:
147
  raise
148
+ if r.status_code == 404 or not r.content:
 
 
149
  return None
150
  r.raise_for_status()
151
  return r.content
 
266
  bigim = None
267
  base_size = [256, 256]
268
  while futures:
269
+ done, _ = concurrent.futures.wait(
270
  futures.keys(), timeout=callback_interval,
271
  return_when=concurrent.futures.FIRST_COMPLETED
272
  )
src/prediction_api/predictors.py CHANGED
@@ -18,7 +18,8 @@ def samexporter_predict(
18
  bbox: llist_float,
19
  prompt: list[dict],
20
  zoom: float,
21
- model_name: str = "fastsam"
 
22
  ) -> Dict[str, int]:
23
  """
24
  Return predictions as a geojson from a geo-referenced image using the given input prompt.
@@ -33,6 +34,7 @@ def samexporter_predict(
33
  prompt: machine learning input prompt
34
  zoom:
35
  model_name: machine learning model name
 
36
 
37
  Returns:
38
  dict: Affine transform
@@ -47,10 +49,10 @@ def samexporter_predict(
47
  app_logger.debug(f"using a {model_name} instance model...")
48
  models_instance = models_dict[model_name]["instance"]
49
 
50
- app_logger.info(f'tile_source: {DEFAULT_TMS}!')
51
  pt0, pt1 = bbox
52
  app_logger.info(f"downloading geo-referenced raster with bbox {bbox}, zoom {zoom}.")
53
- img, matrix = download_extent(DEFAULT_TMS, pt0[0], pt0[1], pt1[0], pt1[1], zoom)
54
  app_logger.info(f"img type {type(img)} with shape/size:{img.size}, matrix:{type(matrix)}, matrix:{matrix}.")
55
 
56
  transform = get_affine_transform_from_gdal(matrix)
 
18
  bbox: llist_float,
19
  prompt: list[dict],
20
  zoom: float,
21
+ model_name: str = "fastsam",
22
+ url_tile: str = DEFAULT_TMS
23
  ) -> Dict[str, int]:
24
  """
25
  Return predictions as a geojson from a geo-referenced image using the given input prompt.
 
34
  prompt: machine learning input prompt
35
  zoom:
36
  model_name: machine learning model name
37
+ url_tile: server url tile
38
 
39
  Returns:
40
  dict: Affine transform
 
49
  app_logger.debug(f"using a {model_name} instance model...")
50
  models_instance = models_dict[model_name]["instance"]
51
 
52
+ app_logger.info(f'tile_source: {url_tile}!')
53
  pt0, pt1 = bbox
54
  app_logger.info(f"downloading geo-referenced raster with bbox {bbox}, zoom {zoom}.")
55
+ img, matrix = download_extent(url_tile, pt0[0], pt0[1], pt1[0], pt1[1], zoom)
56
  app_logger.info(f"img type {type(img)} with shape/size:{img.size}, matrix:{type(matrix)}, matrix:{matrix}.")
57
 
58
  transform = get_affine_transform_from_gdal(matrix)
src/utilities/type_hints.py CHANGED
@@ -4,6 +4,7 @@ from enum import Enum
4
  from pydantic import BaseModel
5
  from typing import TypedDict
6
 
 
7
 
8
  ts_dict_str2 = dict[str, str]
9
  ts_dict_str3 = dict[str, str, any]
@@ -49,3 +50,4 @@ class RawRequestInput(BaseModel):
49
  zoom: int | float
50
  source_type: str = "Satellite"
51
  debug: bool = False
 
 
4
  from pydantic import BaseModel
5
  from typing import TypedDict
6
 
7
+ from src.utilities.constants import DEFAULT_TMS
8
 
9
  ts_dict_str2 = dict[str, str]
10
  ts_dict_str3 = dict[str, str, any]
 
50
  zoom: int | float
51
  source_type: str = "Satellite"
52
  debug: bool = False
53
+ url_tile: str = DEFAULT_TMS
tests/__init__.py CHANGED
@@ -3,3 +3,4 @@ from src import PROJECT_ROOT_FOLDER
3
 
4
  TEST_ROOT_FOLDER = PROJECT_ROOT_FOLDER / "tests"
5
  TEST_EVENTS_FOLDER = TEST_ROOT_FOLDER / "events"
 
 
3
 
4
  TEST_ROOT_FOLDER = PROJECT_ROOT_FOLDER / "tests"
5
  TEST_EVENTS_FOLDER = TEST_ROOT_FOLDER / "events"
6
+ LOCAL_URL_TILE = "http://localhost:8000/lambda_handler/{z}/{x}/{y}.png"
tests/events/geotiff/10/553/394.tif DELETED
Binary file (110 kB)
 
tests/events/geotiff/10/553/395.tif DELETED
Binary file (194 kB)
 
tests/events/geotiff/10/553/396.tif DELETED
Binary file (175 kB)
 
tests/events/geotiff/10/554/394.tif DELETED
Binary file (164 kB)
 
tests/events/geotiff/10/554/395.tif DELETED
Binary file (185 kB)
 
tests/events/geotiff/10/554/396.tif DELETED
Binary file (143 kB)
 
tests/events/geotiff/10/555/394.tif DELETED
Binary file (190 kB)
 
tests/events/geotiff/10/555/395.tif DELETED
Binary file (147 kB)
 
tests/events/geotiff/10/555/396.tif DELETED
Binary file (79 kB)
 
tests/events/lambda_handler.json CHANGED
@@ -52,7 +52,7 @@
52
  "time": "26/Nov/2023:09:58:07 +0000",
53
  "timeEpoch": 1700992687215
54
  },
55
- "body": "{\"bbox\": {\"ne\": {\"lat\": 38.839707613545144, \"lng\": 15.515441894531252},\"sw\": {\"lat\": 38.10430528370985, \"lng\": 14.109191894531252}},\"prompt\": [{\"type\": \"point\",\"data\": {\"lat\": 38.565347844885466, \"lng\": 14.82810974121094},\"label\": 0}],\"zoom\": 10, \"source_type\": \"Satellite\"\n}",
56
  "isBase64Encoded": false
57
  },
58
  "output": {
@@ -60,7 +60,7 @@
60
  "header": {
61
  "Content-Type": "application/json"
62
  },
63
- "body": "{\"n_predictions\": 1, \"geojson\": \"{\\\"type\\\": \\\"FeatureCollection\\\", \\\"features\\\": [{\\\"id\\\": \\\"0\\\", \\\"type\\\": \\\"Feature\\\", \\\"properties\\\": {\\\"raster_val\\\": 255.0}, \\\"geometry\\\": {\\\"type\\\": \\\"Polygon\\\", \\\"coordinates\\\": [[[14.809570312500002, 38.58252615935333], [14.832916259765627, 38.58252615935333], [14.832916259765627, 38.58145263504286], [14.837036132812502, 38.58145263504286], [14.837036132812502, 38.58037909468592], [14.845275878906252, 38.58037909468592], [14.845275878906252, 38.58145263504286], [14.854888916015627, 38.58145263504286], [14.854888916015627, 38.58252615935333], [14.868621826171877, 38.58252615935333], [14.868621826171877, 38.58145263504286], [14.871368408203125, 38.58145263504286], [14.871368408203125, 38.57930553828263], [14.87274169921875, 38.57930553828263], [14.87274169921875, 38.57071650940461], [14.874114990234377, 38.57071650940461], [14.874114990234377, 38.55997877925586], [14.87274169921875, 38.55997877925586], [14.87274169921875, 38.55675714735221], [14.871368408203125, 38.55675714735221], [14.871368408203125, 38.5535353710587], [14.869995117187502, 38.5535353710587], [14.869995117187502, 38.54709138531695], [14.868621826171877, 38.54709138531695], [14.868621826171877, 38.54386917587615], [14.869995117187502, 38.54386917587615], [14.869995117187502, 38.54064682206035], [14.868621826171877, 38.54064682206035], [14.868621826171877, 38.53742432387328], [14.86724853515625, 38.53742432387328], [14.86724853515625, 38.53635012572906], [14.864501953125002, 38.53635012572906], [14.864501953125002, 38.53527591154415], [14.860382080078127, 38.53527591154415], [14.860382080078127, 38.53420168131867], [14.856262207031252, 38.53420168131867], [14.856262207031252, 38.53527591154415], [14.849395751953125, 38.53527591154415], [14.849395751953125, 38.53635012572906], [14.846649169921877, 38.53635012572906], [14.846649169921877, 38.53742432387328], [14.845275878906252, 38.53742432387328], [14.845275878906252, 38.53849850597666], [14.843902587890625, 38.53849850597666], [14.843902587890625, 38.53957267203906], [14.841156005859377, 38.53957267203906], [14.841156005859377, 38.54064682206035], [14.838409423828129, 38.54064682206035], [14.838409423828129, 38.541720956040386], [14.837036132812502, 38.541720956040386], [14.837036132812502, 38.54279507397902], [14.835662841796877, 38.54279507397902], [14.835662841796877, 38.54386917587615], [14.832916259765627, 38.54386917587615], [14.832916259765627, 38.5449432617316], [14.830169677734375, 38.5449432617316], [14.830169677734375, 38.54601733154525], [14.827423095703127, 38.54601733154525], [14.827423095703127, 38.54709138531695], [14.823303222656252, 38.54709138531695], [14.823303222656252, 38.54816542304658], [14.820556640625, 38.54816542304658], [14.820556640625, 38.54923944473399], [14.817810058593752, 38.54923944473399], [14.817810058593752, 38.55031345037904], [14.8150634765625, 38.55031345037904], [14.8150634765625, 38.55138743998161], [14.81231689453125, 38.55138743998161], [14.81231689453125, 38.55246141354154], [14.808197021484377, 38.55246141354154], [14.808197021484377, 38.5535353710587], [14.80682373046875, 38.5535353710587], [14.80682373046875, 38.55460931253297], [14.804077148437502, 38.55460931253297], [14.804077148437502, 38.55568323796418], [14.802703857421875, 38.55568323796418], [14.802703857421875, 38.55675714735221], [14.801330566406254, 38.55675714735221], [14.801330566406254, 38.557831040696925], [14.799957275390623, 38.557831040696925], [14.799957275390623, 38.558904917998184], [14.798583984375002, 38.558904917998184], [14.798583984375002, 38.561052624469795], [14.797210693359375, 38.561052624469795], [14.797210693359375, 38.568569091731305], [14.798583984375002, 38.568569091731305], [14.798583984375002, 38.571790194173666], [14.799957275390623, 38.571790194173666], [14.799957275390623, 38.57286386289749], [14.801330566406254, 38.57286386289749], [14.801330566406254, 38.57501115220886], [14.802703857421875, 38.57501115220886], [14.802703857421875, 38.57823196583315], [14.804077148437502, 38.57823196583315], [14.804077148437502, 38.57930553828263], [14.805450439453125, 38.57930553828263], [14.805450439453125, 38.58037909468592], [14.80682373046875, 38.58037909468592], [14.80682373046875, 38.58145263504286], [14.809570312500002, 38.58145263504286], [14.809570312500002, 38.58252615935333]]]}}, {\\\"id\\\": \\\"1\\\", \\\"type\\\": \\\"Feature\\\", \\\"properties\\\": {\\\"raster_val\\\": 0.0}, \\\"geometry\\\": {\\\"type\\\": \\\"Polygon\\\", \\\"coordinates\\\": [[[14.109191894531252, 38.839707613545166], [14.109191894531252, 38.10430528370981], [15.515441894531252, 38.10430528370981], [15.515441894531252, 38.839707613545166], [14.109191894531252, 38.839707613545166]], [[14.809570312500002, 38.58252615935333], [14.809570312500002, 38.58145263504286], [14.80682373046875, 38.58145263504286], [14.80682373046875, 38.58037909468592], [14.805450439453125, 38.58037909468592], [14.805450439453125, 38.57930553828263], [14.804077148437502, 38.57930553828263], [14.804077148437502, 38.57823196583315], [14.802703857421875, 38.57823196583315], [14.802703857421875, 38.57501115220886], [14.801330566406254, 38.57501115220886], [14.801330566406254, 38.57286386289749], [14.799957275390623, 38.57286386289749], [14.799957275390623, 38.571790194173666], [14.798583984375002, 38.571790194173666], [14.798583984375002, 38.568569091731305], [14.797210693359375, 38.568569091731305], [14.797210693359375, 38.561052624469795], [14.798583984375002, 38.561052624469795], [14.798583984375002, 38.558904917998184], [14.799957275390623, 38.558904917998184], [14.799957275390623, 38.557831040696925], [14.801330566406254, 38.557831040696925], [14.801330566406254, 38.55675714735221], [14.802703857421875, 38.55675714735221], [14.802703857421875, 38.55568323796418], [14.804077148437502, 38.55568323796418], [14.804077148437502, 38.55460931253297], [14.80682373046875, 38.55460931253297], [14.80682373046875, 38.5535353710587], [14.808197021484377, 38.5535353710587], [14.808197021484377, 38.55246141354154], [14.81231689453125, 38.55246141354154], [14.81231689453125, 38.55138743998161], [14.8150634765625, 38.55138743998161], [14.8150634765625, 38.55031345037904], [14.817810058593752, 38.55031345037904], [14.817810058593752, 38.54923944473399], [14.820556640625, 38.54923944473399], [14.820556640625, 38.54816542304658], [14.823303222656252, 38.54816542304658], [14.823303222656252, 38.54709138531695], [14.827423095703127, 38.54709138531695], [14.827423095703127, 38.54601733154525], [14.830169677734375, 38.54601733154525], [14.830169677734375, 38.5449432617316], [14.832916259765627, 38.5449432617316], [14.832916259765627, 38.54386917587615], [14.835662841796877, 38.54386917587615], [14.835662841796877, 38.54279507397902], [14.837036132812502, 38.54279507397902], [14.837036132812502, 38.541720956040386], [14.838409423828129, 38.541720956040386], [14.838409423828129, 38.54064682206035], [14.841156005859377, 38.54064682206035], [14.841156005859377, 38.53957267203906], [14.843902587890625, 38.53957267203906], [14.843902587890625, 38.53849850597666], [14.845275878906252, 38.53849850597666], [14.845275878906252, 38.53742432387328], [14.846649169921877, 38.53742432387328], [14.846649169921877, 38.53635012572906], [14.849395751953125, 38.53635012572906], [14.849395751953125, 38.53527591154415], [14.856262207031252, 38.53527591154415], [14.856262207031252, 38.53420168131867], [14.860382080078127, 38.53420168131867], [14.860382080078127, 38.53527591154415], [14.864501953125002, 38.53527591154415], [14.864501953125002, 38.53635012572906], [14.86724853515625, 38.53635012572906], [14.86724853515625, 38.53742432387328], [14.868621826171877, 38.53742432387328], [14.868621826171877, 38.54064682206035], [14.869995117187502, 38.54064682206035], [14.869995117187502, 38.54386917587615], [14.868621826171877, 38.54386917587615], [14.868621826171877, 38.54709138531695], [14.869995117187502, 38.54709138531695], [14.869995117187502, 38.5535353710587], [14.871368408203125, 38.5535353710587], [14.871368408203125, 38.55675714735221], [14.87274169921875, 38.55675714735221], [14.87274169921875, 38.55997877925586], [14.874114990234377, 38.55997877925586], [14.874114990234377, 38.57071650940461], [14.87274169921875, 38.57071650940461], [14.87274169921875, 38.57930553828263], [14.871368408203125, 38.57930553828263], [14.871368408203125, 38.58145263504286], [14.868621826171877, 38.58145263504286], [14.868621826171877, 38.58252615935333], [14.854888916015627, 38.58252615935333], [14.854888916015627, 38.58145263504286], [14.845275878906252, 38.58145263504286], [14.845275878906252, 38.58037909468592], [14.837036132812502, 38.58037909468592], [14.837036132812502, 38.58145263504286], [14.832916259765627, 38.58145263504286], [14.832916259765627, 38.58252615935333], [14.809570312500002, 38.58252615935333]]]}}]}\", \"n_shapes_geojson\": 2, \"duration_run\": 5.23, \"message\": \"ok\", \"request_id\": \"test_invoke_id\"}",
64
  "isBase64Encoded": false
65
  }
66
  }
 
52
  "time": "26/Nov/2023:09:58:07 +0000",
53
  "timeEpoch": 1700992687215
54
  },
55
+ "body": "{\"bbox\": {\"ne\": {\"lat\": 39.036252959636606, \"lng\": 15.040283203125002}, \"sw\": {\"lat\": 38.302869955150044, \"lng\": 13.634033203125002}}, \"prompt\": [{\"type\": \"point\", \"data\": {\"lat\": 38.48542007717153, \"lng\": 14.921846904165468}, \"label\": 0}], \"zoom\": 10, \"source_type\": \"Satellite\"}",
56
  "isBase64Encoded": false
57
  },
58
  "output": {
 
60
  "header": {
61
  "Content-Type": "application/json"
62
  },
63
+ "body": "{\"n_predictions\": 1, \"geojson\": \"{\\\"type\\\": \\\"FeatureCollection\\\", \\\"features\\\": [{\\\"id\\\": \\\"0\\\", \\\"type\\\": \\\"Feature\\\", \\\"properties\\\": {\\\"raster_val\\\": 255.0}, \\\"geometry\\\": {\\\"type\\\": \\\"Polygon\\\", \\\"coordinates\\\": [[[14.920806884765623, 38.52023522875919], [14.931793212890625, 38.52023522875919], [14.931793212890625, 38.51916077398036], [14.948272705078123, 38.51916077398036], [14.948272705078123, 38.52023522875919], [14.951019287109371, 38.52023522875919], [14.951019287109371, 38.51916077398036], [14.952392578124998, 38.51916077398036], [14.952392578124998, 38.51271370850396], [14.953765869140623, 38.51271370850396], [14.953765869140623, 38.50734071290346], [14.952392578124998, 38.50734071290346], [14.952392578124998, 38.50626606567192], [14.953765869140623, 38.50626606567192], [14.953765869140623, 38.49659351894757], [14.95513916015625, 38.49659351894757], [14.95513916015625, 38.49551871135403], [14.956512451171873, 38.49551871135403], [14.956512451171873, 38.49659351894757], [14.957885742187498, 38.49659351894757], [14.957885742187498, 38.500892588964604], [14.959259033203125, 38.500892588964604], [14.959259033203125, 38.5030420277569], [14.96063232421875, 38.5030420277569], [14.96063232421875, 38.49766831050554], [14.962005615234373, 38.49766831050554], [14.962005615234373, 38.490144432857555], [14.96337890625, 38.490144432857555], [14.96337890625, 38.485844721434205], [14.964752197265625, 38.485844721434205], [14.964752197265625, 38.48369476951686], [14.972991943359375, 38.48369476951686], [14.972991943359375, 38.48261976950727], [14.975738525390623, 38.48261976950727], [14.975738525390623, 38.4815447534639], [14.97711181640625, 38.4815447534639], [14.97711181640625, 38.48046972138692], [14.978485107421875, 38.48046972138692], [14.978485107421875, 38.479394673276445], [14.97711181640625, 38.479394673276445], [14.97711181640625, 38.47616943274548], [14.972991943359375, 38.47616943274548], [14.972991943359375, 38.47509432050244], [14.971618652343748, 38.47509432050244], [14.971618652343748, 38.47401919222662], [14.9688720703125, 38.47401919222662], [14.9688720703125, 38.47186888757716], [14.967498779296873, 38.47186888757716], [14.967498779296873, 38.4707937112038], [14.966125488281248, 38.4707937112038], [14.966125488281248, 38.46971851879821], [14.964752197265625, 38.46971851879821], [14.964752197265625, 38.46864331036053], [14.96337890625, 38.46864331036053], [14.96337890625, 38.465417588856305], [14.962005615234373, 38.465417588856305], [14.962005615234373, 38.46219172306828], [14.96063232421875, 38.46219172306828], [14.96063232421875, 38.46111640240985], [14.959259033203125, 38.46111640240985], [14.959259033203125, 38.46004106572045], [14.957885742187498, 38.46004106572045], [14.957885742187498, 38.458965713000204], [14.959259033203125, 38.458965713000204], [14.959259033203125, 38.456814959467785], [14.96063232421875, 38.456814959467785], [14.96063232421875, 38.45251326003907], [14.957885742187498, 38.45251326003907], [14.957885742187498, 38.4514377951069], [14.953765869140623, 38.4514377951069], [14.953765869140623, 38.450362314145], [14.952392578124998, 38.450362314145], [14.952392578124998, 38.447135775082444], [14.953765869140623, 38.447135775082444], [14.953765869140623, 38.444984668894726], [14.95513916015625, 38.444984668894726], [14.95513916015625, 38.4428334985915], [14.956512451171873, 38.4428334985915], [14.956512451171873, 38.44175788939692], [14.95513916015625, 38.44175788939692], [14.95513916015625, 38.44068226417388], [14.953765869140623, 38.44068226417388], [14.953765869140623, 38.43960662292253], [14.951019287109371, 38.43960662292253], [14.951019287109371, 38.438530965643004], [14.948272705078123, 38.438530965643004], [14.948272705078123, 38.43960662292253], [14.9468994140625, 38.43960662292253], [14.9468994140625, 38.44068226417388], [14.945526123046875, 38.44068226417388], [14.945526123046875, 38.44175788939692], [14.944152832031248, 38.44175788939692], [14.944152832031248, 38.447135775082444], [14.942779541015623, 38.447135775082444], [14.942779541015623, 38.450362314145], [14.940032958984375, 38.450362314145], [14.940032958984375, 38.4514377951069], [14.937286376953121, 38.4514377951069], [14.937286376953121, 38.45251326003907], [14.9359130859375, 38.45251326003907], [14.9359130859375, 38.453588708941375], [14.934539794921873, 38.453588708941375], [14.934539794921873, 38.4546641418137], [14.93316650390625, 38.4546641418137], [14.93316650390625, 38.456814959467785], [14.931793212890625, 38.456814959467785], [14.931793212890625, 38.45789034424927], [14.930419921874998, 38.45789034424927], [14.930419921874998, 38.458965713000204], [14.92767333984375, 38.458965713000204], [14.92767333984375, 38.46004106572045], [14.923553466796873, 38.46004106572045], [14.923553466796873, 38.46111640240985], [14.920806884765623, 38.46111640240985], [14.920806884765623, 38.46219172306828], [14.91668701171875, 38.46219172306828], [14.91668701171875, 38.463267027695586], [14.913940429687496, 38.463267027695586], [14.913940429687496, 38.46434231629164], [14.912567138671875, 38.46434231629164], [14.912567138671875, 38.465417588856305], [14.911193847656252, 38.465417588856305], [14.911193847656252, 38.466492845389425], [14.909820556640625, 38.466492845389425], [14.909820556640625, 38.46756808589088], [14.908447265625, 38.46756808589088], [14.908447265625, 38.46864331036053], [14.907073974609373, 38.46864331036053], [14.907073974609373, 38.46971851879821], [14.905700683593748, 38.46971851879821], [14.905700683593748, 38.47186888757716], [14.904327392578125, 38.47186888757716], [14.904327392578125, 38.47401919222662], [14.9029541015625, 38.47401919222662], [14.9029541015625, 38.47509432050244], [14.901580810546873, 38.47509432050244], [14.901580810546873, 38.47724452895559], [14.900207519531248, 38.47724452895559], [14.900207519531248, 38.490144432857555], [14.901580810546873, 38.490144432857555], [14.901580810546873, 38.49229419236133], [14.9029541015625, 38.49229419236133], [14.9029541015625, 38.498743086027794], [14.904327392578125, 38.498743086027794], [14.904327392578125, 38.50841534409805], [14.905700683593748, 38.50841534409805], [14.905700683593748, 38.511639141458616], [14.907073974609373, 38.511639141458616], [14.907073974609373, 38.51271370850396], [14.908447265625, 38.51271370850396], [14.908447265625, 38.51378825951165], [14.909820556640625, 38.51378825951165], [14.909820556640625, 38.51486279448153], [14.911193847656252, 38.51486279448153], [14.911193847656252, 38.51593731341349], [14.912567138671875, 38.51593731341349], [14.912567138671875, 38.51701181630737], [14.915313720703123, 38.51701181630737], [14.915313720703123, 38.51916077398036], [14.920806884765623, 38.51916077398036], [14.920806884765623, 38.52023522875919]]]}}, {\\\"id\\\": \\\"1\\\", \\\"type\\\": \\\"Feature\\\", \\\"properties\\\": {\\\"raster_val\\\": 0.0}, \\\"geometry\\\": {\\\"type\\\": \\\"Polygon\\\", \\\"coordinates\\\": [[[13.634033203125002, 39.03625295963659], [13.634033203125002, 38.30286995515004], [15.040283203124998, 38.30286995515004], [15.040283203124998, 39.03625295963659], [13.634033203125002, 39.03625295963659]], [[14.920806884765623, 38.52023522875919], [14.920806884765623, 38.51916077398036], [14.915313720703123, 38.51916077398036], [14.915313720703123, 38.51701181630737], [14.912567138671875, 38.51701181630737], [14.912567138671875, 38.51593731341349], [14.911193847656252, 38.51593731341349], [14.911193847656252, 38.51486279448153], [14.909820556640625, 38.51486279448153], [14.909820556640625, 38.51378825951165], [14.908447265625, 38.51378825951165], [14.908447265625, 38.51271370850396], [14.907073974609373, 38.51271370850396], [14.907073974609373, 38.511639141458616], [14.905700683593748, 38.511639141458616], [14.905700683593748, 38.50841534409805], [14.904327392578125, 38.50841534409805], [14.904327392578125, 38.498743086027794], [14.9029541015625, 38.498743086027794], [14.9029541015625, 38.49229419236133], [14.901580810546873, 38.49229419236133], [14.901580810546873, 38.490144432857555], [14.900207519531248, 38.490144432857555], [14.900207519531248, 38.47724452895559], [14.901580810546873, 38.47724452895559], [14.901580810546873, 38.47509432050244], [14.9029541015625, 38.47509432050244], [14.9029541015625, 38.47401919222662], [14.904327392578125, 38.47401919222662], [14.904327392578125, 38.47186888757716], [14.905700683593748, 38.47186888757716], [14.905700683593748, 38.46971851879821], [14.907073974609373, 38.46971851879821], [14.907073974609373, 38.46864331036053], [14.908447265625, 38.46864331036053], [14.908447265625, 38.46756808589088], [14.909820556640625, 38.46756808589088], [14.909820556640625, 38.466492845389425], [14.911193847656252, 38.466492845389425], [14.911193847656252, 38.465417588856305], [14.912567138671875, 38.465417588856305], [14.912567138671875, 38.46434231629164], [14.913940429687496, 38.46434231629164], [14.913940429687496, 38.463267027695586], [14.91668701171875, 38.463267027695586], [14.91668701171875, 38.46219172306828], [14.920806884765623, 38.46219172306828], [14.920806884765623, 38.46111640240985], [14.923553466796873, 38.46111640240985], [14.923553466796873, 38.46004106572045], [14.92767333984375, 38.46004106572045], [14.92767333984375, 38.458965713000204], [14.930419921874998, 38.458965713000204], [14.930419921874998, 38.45789034424927], [14.931793212890625, 38.45789034424927], [14.931793212890625, 38.456814959467785], [14.93316650390625, 38.456814959467785], [14.93316650390625, 38.4546641418137], [14.934539794921873, 38.4546641418137], [14.934539794921873, 38.453588708941375], [14.9359130859375, 38.453588708941375], [14.9359130859375, 38.45251326003907], [14.937286376953121, 38.45251326003907], [14.937286376953121, 38.4514377951069], [14.940032958984375, 38.4514377951069], [14.940032958984375, 38.450362314145], [14.942779541015623, 38.450362314145], [14.942779541015623, 38.447135775082444], [14.944152832031248, 38.447135775082444], [14.944152832031248, 38.44175788939692], [14.945526123046875, 38.44175788939692], [14.945526123046875, 38.44068226417388], [14.9468994140625, 38.44068226417388], [14.9468994140625, 38.43960662292253], [14.948272705078123, 38.43960662292253], [14.948272705078123, 38.438530965643004], [14.951019287109371, 38.438530965643004], [14.951019287109371, 38.43960662292253], [14.953765869140623, 38.43960662292253], [14.953765869140623, 38.44068226417388], [14.95513916015625, 38.44068226417388], [14.95513916015625, 38.44175788939692], [14.956512451171873, 38.44175788939692], [14.956512451171873, 38.4428334985915], [14.95513916015625, 38.4428334985915], [14.95513916015625, 38.444984668894726], [14.953765869140623, 38.444984668894726], [14.953765869140623, 38.447135775082444], [14.952392578124998, 38.447135775082444], [14.952392578124998, 38.450362314145], [14.953765869140623, 38.450362314145], [14.953765869140623, 38.4514377951069], [14.957885742187498, 38.4514377951069], [14.957885742187498, 38.45251326003907], [14.96063232421875, 38.45251326003907], [14.96063232421875, 38.456814959467785], [14.959259033203125, 38.456814959467785], [14.959259033203125, 38.458965713000204], [14.957885742187498, 38.458965713000204], [14.957885742187498, 38.46004106572045], [14.959259033203125, 38.46004106572045], [14.959259033203125, 38.46111640240985], [14.96063232421875, 38.46111640240985], [14.96063232421875, 38.46219172306828], [14.962005615234373, 38.46219172306828], [14.962005615234373, 38.465417588856305], [14.96337890625, 38.465417588856305], [14.96337890625, 38.46864331036053], [14.964752197265625, 38.46864331036053], [14.964752197265625, 38.46971851879821], [14.966125488281248, 38.46971851879821], [14.966125488281248, 38.4707937112038], [14.967498779296873, 38.4707937112038], [14.967498779296873, 38.47186888757716], [14.9688720703125, 38.47186888757716], [14.9688720703125, 38.47401919222662], [14.971618652343748, 38.47401919222662], [14.971618652343748, 38.47509432050244], [14.972991943359375, 38.47509432050244], [14.972991943359375, 38.47616943274548], [14.97711181640625, 38.47616943274548], [14.97711181640625, 38.479394673276445], [14.978485107421875, 38.479394673276445], [14.978485107421875, 38.48046972138692], [14.97711181640625, 38.48046972138692], [14.97711181640625, 38.4815447534639], [14.975738525390623, 38.4815447534639], [14.975738525390623, 38.48261976950727], [14.972991943359375, 38.48261976950727], [14.972991943359375, 38.48369476951686], [14.964752197265625, 38.48369476951686], [14.964752197265625, 38.485844721434205], [14.96337890625, 38.485844721434205], [14.96337890625, 38.490144432857555], [14.962005615234373, 38.490144432857555], [14.962005615234373, 38.49766831050554], [14.96063232421875, 38.49766831050554], [14.96063232421875, 38.5030420277569], [14.959259033203125, 38.5030420277569], [14.959259033203125, 38.500892588964604], [14.957885742187498, 38.500892588964604], [14.957885742187498, 38.49659351894757], [14.956512451171873, 38.49659351894757], [14.956512451171873, 38.49551871135403], [14.95513916015625, 38.49551871135403], [14.95513916015625, 38.49659351894757], [14.953765869140623, 38.49659351894757], [14.953765869140623, 38.50626606567192], [14.952392578124998, 38.50626606567192], [14.952392578124998, 38.50734071290346], [14.953765869140623, 38.50734071290346], [14.953765869140623, 38.51271370850396], [14.952392578124998, 38.51271370850396], [14.952392578124998, 38.51916077398036], [14.951019287109371, 38.51916077398036], [14.951019287109371, 38.52023522875919], [14.948272705078123, 38.52023522875919], [14.948272705078123, 38.51916077398036], [14.931793212890625, 38.51916077398036], [14.931793212890625, 38.52023522875919], [14.920806884765623, 38.52023522875919]]]}}]}\", \"n_shapes_geojson\": 2, \"duration_run\": 7.334042072296143, \"message\": \"ok\", \"request_id\": \"test_invoke_id\"}",
64
  "isBase64Encoded": false
65
  }
66
  }
tests/events/lambda_handler/10/550/391.png ADDED
tests/events/lambda_handler/10/550/392.png ADDED
tests/events/lambda_handler/10/550/393.png ADDED
tests/events/lambda_handler/10/551/391.png ADDED
tests/events/lambda_handler/10/551/392.png ADDED
tests/events/lambda_handler/10/551/393.png ADDED
tests/events/lambda_handler/10/552/391.png ADDED
tests/events/lambda_handler/10/552/392.png ADDED
tests/events/lambda_handler/10/552/393.png ADDED
tests/events/lambda_handler/10/553/391.png ADDED
tests/events/lambda_handler/10/553/392.png ADDED
tests/events/lambda_handler/10/553/393.png ADDED
tests/events/lambda_handler/10/554/391.png ADDED
tests/events/lambda_handler/10/554/392.png ADDED
tests/events/lambda_handler/10/554/393.png ADDED
tests/io/test_tms2geotiff.py CHANGED
@@ -1,4 +1,3 @@
1
- import logging
2
  import unittest
3
 
4
  import numpy as np
@@ -6,51 +5,29 @@ import numpy as np
6
  from src import app_logger
7
  from src.io.tms2geotiff import download_extent
8
  from src.utilities.utilities import hash_calculate
9
- from tests import TEST_EVENTS_FOLDER
10
 
11
 
12
- tile_source_url = "http://localhost:8000/geotiff/{z}/{x}/{y}.tif"
13
- input_bbox = [[38.03932961278458, 15.36808069832851], [37.455509218936974, 14.632807441554068]]
14
 
15
 
16
  class TestTms2geotiff(unittest.TestCase):
17
- from contextlib import contextmanager
18
-
19
- @staticmethod
20
- @contextmanager
21
- def http_server(host: str, port: int, directory: str):
22
- """Function http_server defined within this test class to avoid pytest error "fixture 'host' not found"."""
23
- from functools import partial
24
- from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
25
- from threading import Thread
26
-
27
- server = ThreadingHTTPServer(
28
- (host, port), partial(SimpleHTTPRequestHandler, directory=directory)
29
- )
30
- server_thread = Thread(target=server.serve_forever, name="http_server")
31
- server_thread.start()
32
- logging.info(f"listen:: host {host}, port {port}.")
33
-
34
- try:
35
- yield
36
- finally:
37
- server.shutdown()
38
- server_thread.join()
39
-
40
  def test_download_extent(self):
 
 
41
  listen_port = 8000
42
 
43
- with self.http_server("localhost", listen_port, directory=TEST_EVENTS_FOLDER):
44
  pt0, pt1 = input_bbox
45
  zoom = 10
46
  img, matrix = download_extent(
47
- source=tile_source_url, lat0=pt0[0], lon0=pt0[1], lat1=pt1[0], lon1=pt1[1], zoom=zoom
48
  )
49
  app_logger.info("# DOWNLOAD ENDED! #")
50
  np_img = np.array(img)
51
  output_hash = hash_calculate(np_img)
52
- assert output_hash == b'LJNhEuMMp2nRclFJfF6oM3iMVbnZnWDmZqWzrs3T4Hs='
53
- assert matrix == (1628916.673269823, 76.42413122132838, 0, 4584983.258896411, 0, -76.45836079647981)
54
 
55
  def test_download_extent_io_error1(self):
56
 
@@ -59,11 +36,11 @@ class TestTms2geotiff(unittest.TestCase):
59
  pt0, pt1 = input_bbox
60
  zoom = 10
61
  download_extent(
62
- source=tile_source_url, lat0=pt0[0], lon0=pt0[1], lat1=pt1[0], lon1=pt1[1], zoom=zoom
63
  )
64
  except IOError as ioe1:
65
  app_logger.error(f"ioe1:{ioe1}.")
66
- msg0 = "HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /geotiff/"
67
  msg1 = "Caused by NewConnectionError"
68
  msg2 = ": Failed to establish a new connection: [Errno 61] Connection refused'))"
69
  assert msg0 in str(ioe1)
@@ -72,28 +49,20 @@ class TestTms2geotiff(unittest.TestCase):
72
  raise ioe1
73
 
74
  def test_download_extent_io_error2(self):
 
 
75
  listen_port = 8000
76
- with self.http_server("localhost", listen_port, directory=TEST_EVENTS_FOLDER):
77
  pt0, pt1 = input_bbox
78
  zoom = 10
79
 
80
  with self.assertRaises(AttributeError):
81
  try:
82
  download_extent(
83
- source=tile_source_url + "_not_found_raster!",
84
  lat0=pt0[0], lon0=pt0[1], lat1=pt1[0], lon1=pt1[1], zoom=zoom
85
  )
86
  except AttributeError as ae:
87
  app_logger.error(f"ae:{ae}.")
88
  assert str(ae) == "'NoneType' object has no attribute 'crop'"
89
  raise ae
90
-
91
-
92
- if __name__ == '__main__':
93
- from tests import TEST_ROOT_FOLDER
94
-
95
- main_listen_port = 8000
96
- logging.info(f"http_basedir_serve: {TEST_ROOT_FOLDER}.")
97
- with TestTms2geotiff.http_server("127.0.0.1", main_listen_port, directory=TEST_ROOT_FOLDER):
98
- logging.info("""import time; time.sleep(10)""")
99
- logging.info("Http server stopped.")
 
 
1
  import unittest
2
 
3
  import numpy as np
 
5
  from src import app_logger
6
  from src.io.tms2geotiff import download_extent
7
  from src.utilities.utilities import hash_calculate
8
+ from tests import LOCAL_URL_TILE, TEST_EVENTS_FOLDER
9
 
10
 
11
+ input_bbox = [[39.036252959636606, 15.040283203125002], [38.302869955150044, 13.634033203125002]]
 
12
 
13
 
14
  class TestTms2geotiff(unittest.TestCase):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def test_download_extent(self):
16
+ from tests.local_tiles_http_server import LocalTilesHttpServer
17
+
18
  listen_port = 8000
19
 
20
+ with LocalTilesHttpServer.http_server("localhost", listen_port, directory=TEST_EVENTS_FOLDER):
21
  pt0, pt1 = input_bbox
22
  zoom = 10
23
  img, matrix = download_extent(
24
+ source=LOCAL_URL_TILE, lat0=pt0[0], lon0=pt0[1], lat1=pt1[0], lon1=pt1[1], zoom=zoom
25
  )
26
  app_logger.info("# DOWNLOAD ENDED! #")
27
  np_img = np.array(img)
28
  output_hash = hash_calculate(np_img)
29
+ assert output_hash == b'Rd95Whd3nP4PW4pgcYsoyTqUUabpt8LfYxns022em7o='
30
+ assert matrix == (1517733.63363046, 152.8740565703522, 0, 4726865.829155299, 0, -152.87405657035038)
31
 
32
  def test_download_extent_io_error1(self):
33
 
 
36
  pt0, pt1 = input_bbox
37
  zoom = 10
38
  download_extent(
39
+ source=LOCAL_URL_TILE, lat0=pt0[0], lon0=pt0[1], lat1=pt1[0], lon1=pt1[1], zoom=zoom
40
  )
41
  except IOError as ioe1:
42
  app_logger.error(f"ioe1:{ioe1}.")
43
+ msg0 = "HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /lambda_handler"
44
  msg1 = "Caused by NewConnectionError"
45
  msg2 = ": Failed to establish a new connection: [Errno 61] Connection refused'))"
46
  assert msg0 in str(ioe1)
 
49
  raise ioe1
50
 
51
  def test_download_extent_io_error2(self):
52
+ from tests.local_tiles_http_server import LocalTilesHttpServer
53
+
54
  listen_port = 8000
55
+ with LocalTilesHttpServer.http_server("localhost", listen_port, directory=TEST_EVENTS_FOLDER):
56
  pt0, pt1 = input_bbox
57
  zoom = 10
58
 
59
  with self.assertRaises(AttributeError):
60
  try:
61
  download_extent(
62
+ source=LOCAL_URL_TILE + "_not_found_raster!",
63
  lat0=pt0[0], lon0=pt0[1], lat1=pt1[0], lon1=pt1[1], zoom=zoom
64
  )
65
  except AttributeError as ae:
66
  app_logger.error(f"ae:{ae}.")
67
  assert str(ae) == "'NoneType' object has no attribute 'crop'"
68
  raise ae
 
 
 
 
 
 
 
 
 
 
tests/local_tiles_http_server.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import time
3
+ import unittest
4
+
5
+
6
+ class LocalTilesHttpServer(unittest.TestCase):
7
+ from contextlib import contextmanager
8
+
9
+ @staticmethod
10
+ @contextmanager
11
+ def http_server(host: str, port: int, directory: str):
12
+ """Function http_server defined within this test class to avoid pytest error "fixture 'host' not found"."""
13
+ from functools import partial
14
+ from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
15
+ from threading import Thread
16
+
17
+ server = ThreadingHTTPServer(
18
+ (host, port), partial(SimpleHTTPRequestHandler, directory=directory)
19
+ )
20
+ print("dir:", directory, "#")
21
+ server_thread = Thread(target=server.serve_forever, name="http_server")
22
+ server_thread.start()
23
+ logging.info(f"listen:: host {host}, port {port}.")
24
+
25
+ try:
26
+ yield
27
+ finally:
28
+ server.shutdown()
29
+ server_thread.join()
30
+
31
+
32
+ if __name__ == '__main__':
33
+ # from tests import TEST_ROOT_FOLDER
34
+ from pathlib import Path
35
+
36
+ PROJECT_ROOT_FOLDER = Path(globals().get("__file__", "./_")).absolute().parent.parent
37
+
38
+ TEST_ROOT_FOLDER = PROJECT_ROOT_FOLDER / "tests"
39
+ TEST_EVENTS_FOLDER = TEST_ROOT_FOLDER / "events"
40
+
41
+ main_listen_port = 8000
42
+ logging.info(f"http_basedir_serve: {TEST_ROOT_FOLDER}.")
43
+ with LocalTilesHttpServer.http_server("localhost", main_listen_port, directory=str(TEST_ROOT_FOLDER)):
44
+ time.sleep(1000)
45
+ logging.info("""import time; time.sleep(10)""")
46
+ # logging.info("Http server stopped.")
tests/test_app.py CHANGED
@@ -6,6 +6,7 @@ from unittest.mock import patch
6
  from awslambdaric.lambda_context import LambdaContext
7
 
8
  from src import app
 
9
 
10
 
11
  class TestAppFailures(unittest.TestCase):
@@ -148,7 +149,7 @@ class TestAppFailures(unittest.TestCase):
148
  import shapely
149
 
150
  from src.app import lambda_handler
151
- from tests import TEST_EVENTS_FOLDER
152
 
153
  name_fn = "lambda_handler"
154
  invoke_id = "test_invoke_id"
@@ -162,8 +163,17 @@ class TestAppFailures(unittest.TestCase):
162
  epoch_deadline_time_in_ms=time.time()
163
  )
164
  expected_response_dict = inputs_outputs["output"]
 
 
165
  expected_response_body = json.loads(expected_response_dict["body"])
166
- response = lambda_handler(event=inputs_outputs["input"], context=lambda_context)
 
 
 
 
 
 
 
167
 
168
  response_dict = json.loads(response)
169
  body_dict = json.loads(response_dict["body"])
@@ -172,5 +182,5 @@ class TestAppFailures(unittest.TestCase):
172
  assert body_dict["message"] == "ok"
173
  assert body_dict["n_shapes_geojson"] == expected_response_body["n_shapes_geojson"]
174
  output_geojson = shapely.from_geojson(body_dict["geojson"])
175
- expected_output_geojson = shapely.from_geojson(expected_response_body["geojson"])
176
  assert shapely.equals_exact(output_geojson, expected_output_geojson, tolerance=0.000006)
 
6
  from awslambdaric.lambda_context import LambdaContext
7
 
8
  from src import app
9
+ from tests.local_tiles_http_server import LocalTilesHttpServer
10
 
11
 
12
  class TestAppFailures(unittest.TestCase):
 
149
  import shapely
150
 
151
  from src.app import lambda_handler
152
+ from tests import LOCAL_URL_TILE, TEST_EVENTS_FOLDER
153
 
154
  name_fn = "lambda_handler"
155
  invoke_id = "test_invoke_id"
 
163
  epoch_deadline_time_in_ms=time.time()
164
  )
165
  expected_response_dict = inputs_outputs["output"]
166
+ listen_port = 8000
167
+
168
  expected_response_body = json.loads(expected_response_dict["body"])
169
+ expected_output_geojson = shapely.from_geojson(expected_response_body["geojson"])
170
+
171
+ with LocalTilesHttpServer.http_server("localhost", listen_port, directory=TEST_EVENTS_FOLDER):
172
+ input_event = inputs_outputs["input"]
173
+ input_event_body = json.loads(input_event["body"])
174
+ input_event_body["url_tile"] = LOCAL_URL_TILE
175
+ input_event["body"] = json.dumps(input_event_body)
176
+ response = lambda_handler(event=input_event, context=lambda_context)
177
 
178
  response_dict = json.loads(response)
179
  body_dict = json.loads(response_dict["body"])
 
182
  assert body_dict["message"] == "ok"
183
  assert body_dict["n_shapes_geojson"] == expected_response_body["n_shapes_geojson"]
184
  output_geojson = shapely.from_geojson(body_dict["geojson"])
185
+
186
  assert shapely.equals_exact(output_geojson, expected_output_geojson, tolerance=0.000006)