11"""A Project is a collection of zero or more scenes"""
2+ from .. import NOTEBOOK_SUPPORT
3+
4+ if NOTEBOOK_SUPPORT :
5+ from ipyleaflet import (
6+ Map ,
7+ SideBySideControl ,
8+ TileLayer ,
9+ )
10+
11+ from ..decorators import check_notebook # NOQA
212
313
414class Project (object ):
@@ -23,6 +33,75 @@ def __init__(self, project, api):
2333 self .name = project .name
2434 self .id = project .id
2535
36+ @check_notebook
37+ def get_map (self , ** kwargs ):
38+ """Return an ipyleaflet map centered on this project's center
39+
40+ Args:
41+ **kwargs: additional arguments to pass to Map initializations
42+ """
43+ default_url = (
44+ 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/'
45+ 'light_all/{z}/{x}/{y}.png'
46+ )
47+ return Map (
48+ default_tiles = TileLayer (url = kwargs .get ('url' , default_url )),
49+ center = self .get_center (),
50+ scroll_wheel_zoom = kwargs .get ('scroll_wheel_zoom' , True ),
51+ ** kwargs
52+ )
53+
54+ @check_notebook
55+ def add_to (self , leaflet_map ):
56+ """Add this project to a leaflet map
57+
58+ Args:
59+ leaflet_map (Map): map to add this layer to
60+ """
61+
62+ leaflet_map .add_layer (self .get_layer ())
63+
64+ @check_notebook
65+ def compare (self , other , leaflet_map ):
66+ """Add a slider to compare two projects
67+
68+ This project determines the map center.
69+
70+ Args:
71+ other (Project): the project to compare with this project
72+ leaflet_map (Map): map to add the slider to
73+ """
74+
75+ control = SideBySideControl (
76+ leftLayer = self .get_layer (), rightLayer = other .get_layer ()
77+ )
78+ leaflet_map .add_control (control )
79+
80+ def get_center (self ):
81+ """Get the center of this project's extent"""
82+ coords = self ._project .extent .get ('coordinates' )
83+ if not coords :
84+ raise ValueError (
85+ 'Project must have coordinates to calculate a center'
86+ )
87+ x_min = min (
88+ coord [0 ] + (360 if coord [0 ] < 0 else 0 ) for coord in coords [0 ]
89+ )
90+ x_max = max (
91+ coord [0 ] + (360 if coord [0 ] < 0 else 0 ) for coord in coords [0 ]
92+ )
93+ y_min = min (coord [1 ] for coord in coords [0 ])
94+ y_max = max (coord [1 ] for coord in coords [0 ])
95+ center = [(y_min + y_max ) / 2. , (x_min + x_max ) / 2. ]
96+ if center [0 ] > 180 :
97+ center [0 ] = center [0 ] - 360
98+ return tuple (center )
99+
100+ @check_notebook
101+ def get_layer (self ):
102+ """Returns a TileLayer for display using ipyleaflet"""
103+ return TileLayer (url = self .tms ())
104+
26105 def tms (self ):
27106 """Return a TMS URL for a project"""
28107
0 commit comments