Skip to content

Commit

Permalink
show 🌎 in epsg4326 projection
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzhak committed May 8, 2017
1 parent d98d460 commit cd8cc16
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ services:
ports:
- "8080:8080"
volumes:
- .:/usr/src/app
- .:/usr/src/app:nocopy
depends_on:
- mongo
Empty file added nasabot/geo/__init__.py
Empty file.
45 changes: 45 additions & 0 deletions nasabot/geo/tiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import math


def mercator_tile_by_coords(lat_deg, lon_deg, level):
"""
epsg3857
:param lat_deg:
:param lon_deg:
:param level:
:return:
"""
lat_rad = math.radians(lat_deg)
zoom = 2.0 ** level
xtile = int((lon_deg + 180.0) / 360.0 * zoom)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * zoom)
return {
'x': xtile,
'y': ytile,
}


def wgs84_tile_by_coors(lat, long, level):
"""
epsg4326
actually here should be 180 but....
For GIBS GCS, the level 0 resolution is 288 deg / tile, not 180 deg/tile.
It was chosen to approximate the full resolution of the MODIS images,
avoiding oversampling or undersampling, while still having full width
tiles at most levels (2 and up).
more is here <https://github.com/nasa-gibs/onearth/issues/53#issuecomment-299738858>
:param lat:
:param long:
:param level:
:return:
"""
magic_number = 1 / 288
zoom = 2 ** level
return {
'x': int((90 - lat) * zoom * magic_number),
'y': int((180 + long) * zoom * magic_number),
}
61 changes: 32 additions & 29 deletions nasabot/query/query_stories.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
from botstory.middlewares import any, location, option, sticker, text
from botstory.ast.story_context import get_message_attachment
import emoji
from datetime import datetime, timedelta
from datetime import date, timedelta
import logging
from nasabot.geo import tiles

logger = logging.getLogger(__name__)

# satellite_image = 'https://gibs.earthdata.nasa.gov/wmts/epsg4326/best/MODIS_Terra_CorrectedReflectance_TrueColor/default/{date}/250m/{z}/{y}/{x}.jpg'
satellite_image = 'https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/MODIS_Terra_CorrectedReflectance_TrueColor/default/2017-04-12/GoogleMapsCompatible_Level9/{z}/{y}/{x}.jpg'

import math


def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return {
'x': xtile,
'y': ytile,
'z': n,
}
satellite_image_epsg3857 = 'https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/MODIS_Terra_CorrectedReflectance_TrueColor/default/{date}/GoogleMapsCompatible_Level9/{z}/{y}/{x}.jpg'
satellite_image_epsg4326 = 'https://gibs.earthdata.nasa.gov/wmts/epsg4326/best/MODIS_Terra_CorrectedReflectance_TrueColor/default/{date}/250m/{z}/{y}/{x}.jpg'


def day_before():
return datetime.today() - timedelta(days=1)
return date.today() - timedelta(days=1)


def setup(story):
async def show_image(ctx, target_date, lat, long, zoom):
tile = deg2num(
lat, long, zoom,
)
async def show_image(ctx, target_date, lat, long, level):
tile = tiles.wgs84_tile_by_coors(lat, long, level)
await story.send_image(
satellite_image.format(
# satellite_image_epsg3857.format(
satellite_image_epsg4326.format(
**tile,
date=target_date.isoformat(),
x=tile['x'],
y=tile['y'],
z=zoom,
z=level,
),
user=ctx['user'],
)
Expand Down Expand Up @@ -70,7 +55,7 @@ async def show_whole_earth(ctx):
def handle_america_location():
@story.part()
async def show_america(ctx):
await show_image(ctx, day_before(), 5, -45, 2)
await show_image(ctx, day_before(), 5, -90, 2)

@story.on(emoji.emojize(':earth_africa:', use_aliases=True))
def handle_africa_location():
Expand All @@ -82,7 +67,25 @@ async def show_africa_n_europe_(ctx):
def handle_asia_location():
@story.part()
async def show_asia(ctx):
await show_image(ctx, day_before(), 0, 45, 2)
await show_image(ctx, day_before(), 0, 170, 2)

@story.on(text.Any())
def handle_list_of_coords():
@story.part()
async def use_passed_coords_to_show_earth(ctx):
raw_text = text.get_raw_text(ctx)
values = raw_text.split(',')
if len(values) < 2 or len(values) > 4:
raise NotImplemented('Should parse if got less then 2 or more the 4 values with , delimiter')

lat = float(values[0])
long = float(values[1])
if len(values) > 2:
zoom = int(values[2])
else:
zoom = 6

await show_image(ctx, day_before(), lat, long, zoom)

@story.on(location.Any())
def handle_location():
Expand All @@ -93,4 +96,4 @@ async def show_earth_of_location(ctx):

# TODO: request zoom from User
# TODO: request target date
await show_image(ctx, day_before(), location['lat'], location['long'], 8)
await show_image(ctx, day_before(), location['lat'], location['long'], 5)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pytest-asyncio==0.5.0
pytest-catchlog==1.2.2
pytest-cov==2.4.0
pytest-mock==1.6.0
python-slugify==1.2.4

0 comments on commit cd8cc16

Please sign in to comment.