Skip to content

Commit feb13a8

Browse files
committed
fix: longitudes to set range
fix: disable tkinter file loaders on binder fix: disable geometry editing until ipyleaflet update feat: geometry area units in metric
1 parent b71867a commit feb13a8

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

sliderule/io.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ def winding(x,y):
173173
wind = np.sum([(x[i+1] - x[i])*(y[i+1] + y[i]) for i in range(npts - 1)])
174174
return wind
175175

176+
# fix longitudes to be -180:180
177+
def wrap_longitudes(lon):
178+
phi = np.arctan2(np.sin(lon*np.pi/180.0),np.cos(lon*np.pi/180.0))
179+
# convert phi from radians to degrees
180+
return phi*180.0/np.pi
181+
176182
# convert coordinates to a sliderule region
177183
def to_region(lon,lat):
178184
region = [{'lon':ln,'lat':lt} for ln,lt in np.c_[lon,lat]]

sliderule/ipysliderule.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2828
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30+
import os
3031
import copy
3132
import datetime
3233
import ipywidgets
@@ -223,10 +224,13 @@ def __init__(self):
223224
self.savebutton.on_click(self.saveas_file)
224225
self.savelabel.observe(self.set_savefile)
225226
# create hbox of file selection
226-
self.filesaver = ipywidgets.HBox([
227-
self.savebutton,
228-
self.savelabel
229-
])
227+
if os.environ.get("DISPLAY"):
228+
self.filesaver = ipywidgets.HBox([
229+
self.savebutton,
230+
self.savelabel
231+
])
232+
else:
233+
self.filesaver = copy.copy(self.savelabel)
230234

231235
# button and label for input file selection
232236
self.loadbutton = ipywidgets.Button(
@@ -240,10 +244,13 @@ def __init__(self):
240244
self.loadbutton.on_click(self.select_file)
241245
self.loadlabel.observe(self.set_loadfile)
242246
# create hbox of file selection
243-
self.fileloader = ipywidgets.HBox([
244-
self.loadbutton,
245-
self.loadlabel
246-
])
247+
if os.environ.get("DISPLAY"):
248+
self.fileloader = ipywidgets.HBox([
249+
self.loadbutton,
250+
self.loadlabel
251+
])
252+
else:
253+
self.fileloader = copy.copy(self.loadlabel)
247254

248255
def saveas_file(self, b):
249256
"""function for file save
@@ -457,10 +464,13 @@ def __init__(self, projection, **kwargs):
457464
# keep track of cursor position
458465
self.map.on_interaction(self.handle_interaction)
459466
# add control for drawing polygons or bounding boxes
460-
draw_control = ipyleaflet.DrawControl(polyline={},circlemarker={})
467+
draw_control = ipyleaflet.DrawControl(polyline={},circlemarker={},
468+
edit=False)
461469
shapeOptions = {'color':kwargs['color'],'fill_color':kwargs['color']}
462-
draw_control.rectangle = dict(shapeOptions=shapeOptions)
463-
draw_control.polygon = dict(shapeOptions=shapeOptions)
470+
draw_control.rectangle = dict(shapeOptions=shapeOptions,
471+
metric=['km','m'])
472+
draw_control.polygon = dict(shapeOptions=shapeOptions,
473+
allowIntersection=False,showArea=True,metric=['km','m'])
464474
# create regions
465475
self.regions = []
466476
draw_control.on_draw(self.handle_draw)
@@ -469,12 +479,15 @@ def __init__(self, projection, **kwargs):
469479
# handle cursor movements for label
470480
def handle_interaction(self, **kwargs):
471481
if (kwargs.get('type') == 'mousemove'):
482+
lat,lon = kwargs.get('coordinates')
483+
lon = sliderule.io.wrap_longitudes(lon)
472484
self.cursor.value = u"""Latitude: {d[0]:8.4f}\u00B0,
473-
Longitude: {d[1]:8.4f}\u00B0""".format(d=kwargs.get('coordinates'))
485+
Longitude: {d[1]:8.4f}\u00B0""".format(d=[lat,lon])
474486

475487
# keep track of rectangles and polygons drawn on map
476488
def handle_draw(self, obj, action, geo_json):
477489
lon,lat = np.transpose(geo_json['geometry']['coordinates'])
490+
lon = sliderule.io.wrap_longitudes(lon)
478491
cx,cy = sliderule.io.centroid(lon,lat)
479492
wind = sliderule.io.winding(lon,lat)
480493
# set winding to counter-clockwise
@@ -489,3 +502,4 @@ def handle_draw(self, obj, action, geo_json):
489502
elif (action == 'deleted'):
490503
self.regions.remove(region)
491504
return self
505+

0 commit comments

Comments
 (0)