Skip to content

Commit 9a200e0

Browse files
authored
Merge pull request #603 from NhienPhan2909/map_test
Increase the test coverage of maps.py in test_maps.py
2 parents e5a74eb + 20b19fe commit 9a200e0

File tree

3 files changed

+399
-59
lines changed

3 files changed

+399
-59
lines changed

datascience/maps.py

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,16 @@ def _autozoom(self):
217217
# remove the following with new Folium release
218218
# rough approximation, assuming max_zoom is 18
219219
import math
220-
try:
221-
lat_diff = bounds['max_lat'] - bounds['min_lat']
222-
lon_diff = bounds['max_lon'] - bounds['min_lon']
223-
area, max_area = lat_diff*lon_diff, 180*360
224-
if area:
225-
factor = 1 + max(0, 1 - self._width/1000)/2 + max(0, 1-area**0.5)/2
226-
zoom = math.log(area/max_area)/-factor
227-
else:
228-
zoom = self._default_zoom
229-
zoom = max(1, min(18, round(zoom)))
230-
attrs['zoom_start'] = zoom
231-
except ValueError as e:
232-
raise Exception('Check that your locations are lat-lon pairs', e)
220+
lat_diff = bounds['max_lat'] - bounds['min_lat']
221+
lon_diff = bounds['max_lon'] - bounds['min_lon']
222+
area, max_area = lat_diff*lon_diff, 180*360
223+
if area:
224+
factor = 1 + max(0, 1 - self._width/1000)/2 + max(0, 1-area**0.5)/2
225+
zoom = math.log(area/max_area)/-factor
226+
else:
227+
zoom = self._default_zoom
228+
zoom = max(1, min(18, round(zoom)))
229+
attrs['zoom_start'] = zoom
233230

234231
return attrs
235232

@@ -395,20 +392,21 @@ def read_geojson(cls, path_or_json_or_string_or_url):
395392
data = None
396393
if isinstance(path_or_json_or_string_or_url, (dict, list)):
397394
data = path_or_json_or_string_or_url
398-
try:
399-
data = json.loads(path_or_json_or_string_or_url)
400-
except ValueError:
401-
pass
402-
try:
403-
path = path_or_json_or_string_or_url
404-
if path.endswith('.gz') or path.endswith('.gzip'):
405-
import gzip
406-
contents = gzip.open(path, 'r').read().decode('utf-8')
407-
else:
408-
contents = open(path, 'r').read()
409-
data = json.loads(contents)
410-
except FileNotFoundError:
411-
pass
395+
else:
396+
try:
397+
data = json.loads(path_or_json_or_string_or_url)
398+
except ValueError:
399+
pass
400+
try:
401+
path = path_or_json_or_string_or_url
402+
if path.endswith('.gz') or path.endswith('.gzip'):
403+
import gzip
404+
contents = gzip.open(path, 'r').read().decode('utf-8')
405+
else:
406+
contents = open(path, 'r').read()
407+
data = json.loads(contents)
408+
except FileNotFoundError:
409+
pass
412410
if not data:
413411
import urllib.request
414412
with urllib.request.urlopen(path_or_json_or_string_or_url) as url:
@@ -425,7 +423,7 @@ def _read_geojson_features(data, features=None, prefix=""):
425423
key = feature.get('id', prefix + str(i))
426424
feature_type = feature['geometry']['type']
427425
if feature_type == 'FeatureCollection':
428-
_read_geojson_features(feature, features, prefix + '.' + key)
426+
value = Map._read_geojson_features(feature['geometry'], features, prefix + '.' + key)
429427
elif feature_type == 'Point':
430428
value = Circle._convert_point(feature)
431429
elif feature_type in ['Polygon', 'MultiPolygon']:
@@ -575,7 +573,7 @@ def _convert_point(cls, feature):
575573
"""Convert a GeoJSON point to a Marker."""
576574
lon, lat = feature['geometry']['coordinates']
577575
popup = feature['properties'].get('name', '')
578-
return cls(lat, lon)
576+
return cls(lat, lon, popup=popup)
579577

580578
@classmethod
581579
def map(cls, latitudes, longitudes, labels=None, colors=None, areas=None, other_attrs=None, clustered_marker=False, **kwargs):
@@ -855,7 +853,7 @@ def polygons(self):
855853
"""
856854
if self.type == 'Polygon':
857855
polygons = [self._geojson['geometry']['coordinates']]
858-
elif self.type == 'MultiPolygon':
856+
else: # self.type == "MultiPolygon"
859857
polygons = self._geojson['geometry']['coordinates']
860858
return [ [ [_lat_lons_from_geojson(s) for
861859
s in ring ] for
@@ -979,11 +977,7 @@ def get_coordinates(table, replace_columns=False, remove_nans=False):
979977
table = table.with_columns("lat", lat, "lon", lon)
980978
table = table.drop(index_name)
981979
if replace_columns:
982-
for label in ["county", "city", "zip code", "state"]:
983-
try:
984-
table = table.drop(label)
985-
except KeyError:
986-
pass
980+
table = table.drop(["county", "city", "zip code", "state"])
987981
if remove_nans:
988982
table = table.where("lat", are.below(float("inf"))) # NaNs are not considered to be smaller than infinity
989983
return table

0 commit comments

Comments
 (0)