Error and kernel dies with shapely/geos inside Jupyter notebook

المشرف العام

Administrator
طاقم الإدارة
I have a Python 2.7 script (code below) that scrapes data from a table on the web and draws a map. It runs fine from the command line using

$ python bears.pyWhen I run the identical script from inside a Jupyter notebook, the kernel dies (and apparently restarts) and I get this error:

ERROR:shapely.geos:Geometry must be a Point or LineStringERROR:shapely.geos:Geometry must be a Point or LineStringI am on a MacBook Pro using OS X 10.9.5. My Python environment is the free Anaconda distribution.

Thanks for any help. I am new to cartopy/shapely/geos so let me know if more/different info is needed.

Script:

import reimport numpy as npimport matplotlib as mplimport matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.io.shapereader as shpreaderimport requestsfrom bs4 import BeautifulSoup, Tag# scrape bear sightings dataBEAR_SIGHTINGS_URL = 'http://www.depdata.ct.gov/wildlife/sighting/bearsight.asp'r = requests.get(BEAR_SIGHTINGS_URL)soup = BeautifulSoup(r.content, "html.parser")# extract time period covered by the bear sightingsdate = '\d{1,2}/\d{1,2}/\d{4}'pattern = date + ' to ' + dateresult = re.search(pattern, str(soup))# create dict of all 169 towns in Conn.towns = {}with open("towns.d") as t: for line in t: key = line.strip() towns[key] = 0# extract sightings data and enter in towns dict.# towns without any sightings are not listed on the state web page# help from: stackoverflow.com/questions/16982253/# beautifulsoup-return-next-sibling-after-using-findalltextbear_sightings = {}for t in soup.find_all(width="75%", valign="middle", align="left"): twn = t.find('font').string.strip() for item in t.next_siblings: if isinstance(item, Tag): num = item.find('font').string.strip() towns[twn] = int(num)# draw the map and colorbarfig = plt.figure(figsize=(6, 6))ax1 = fig.add_axes([0, 0, 1, 1], frameon=False, projection=ccrs.Mercator())ax1.set_extent([-74, -71.5, 40.5, 42.25], ccrs.Geodetic())ax1.outline_patch.set_visible(False)ax2 = fig.add_axes([0.4, 0.33, 0.45, 0.03])cmap = plt.cm.RdYlBu_rbounds = np.linspace(0, 500, 11)norm = mpl.colors.BoundaryNorm(bounds, cmap.N)shape_file_name = 'townct_37800_0000_1984_s24_ctdep_1_shp/WGS84/townct_37800_0000_1984_s24_ctdep_1_shp_wgs84.shp'towns_shp = shpreader.Reader(shape_file_name)for this_town in towns_shp.records(): raw_face_color = towns[this_town.attributes['TOWN']] ax1.add_geometries(this_town.geometry, ccrs.PlateCarree(), facecolor=cmap(raw_face_color))cb = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm, ticks=bounds, spacing='uniform', orientation='horizontal')cb.set_label('Number of bear sightings')cb.ax.tick_params(labelsize=7)sup_title_string = 'test2 Black bear sightings in Connecticut\n %s' % result.group()fig.suptitle(sup_title_string,fontsize=18)#plt.show()plt.savefig('bears.png', dpi=fig.dpi)

أكثر...
 
أعلى