|
41 | 41 | import geopandas
|
42 | 42 | from shapely.geometry.multipolygon import MultiPolygon
|
43 | 43 | from shapely.geometry import Polygon
|
44 |
| -from sklearn.cluster import KMeans |
45 | 44 | import sliderule
|
46 | 45 |
|
47 | 46 | ###############################################################################
|
|
51 | 50 | # create logger
|
52 | 51 | logger = logging.getLogger(__name__)
|
53 | 52 |
|
| 53 | +# import cluster support |
| 54 | +clustering_enabled = False |
| 55 | +try: |
| 56 | + from sklearn.cluster import KMeans |
| 57 | + clustering_enabled = True |
| 58 | +except: |
| 59 | + logger.warning("Unable to import sklearn... clustering support disabled") |
| 60 | + |
54 | 61 | # profiling times for each major function
|
55 | 62 | profiles = {}
|
56 | 63 |
|
@@ -1327,22 +1334,25 @@ def toregion(source, tolerance=0.0, cellsize=0.01, n_clusters=1):
|
1327 | 1334 | # generate clusters
|
1328 | 1335 | clusters = []
|
1329 | 1336 | if n_clusters > 1:
|
1330 |
| - # pull out centroids of each geometry object |
1331 |
| - if "CenLon" in gdf and "CenLat" in gdf: |
1332 |
| - X = numpy.column_stack((gdf["CenLon"], gdf["CenLat"])) |
| 1337 | + if clustering_enabled: |
| 1338 | + # pull out centroids of each geometry object |
| 1339 | + if "CenLon" in gdf and "CenLat" in gdf: |
| 1340 | + X = numpy.column_stack((gdf["CenLon"], gdf["CenLat"])) |
| 1341 | + else: |
| 1342 | + s = gdf.centroid |
| 1343 | + X = numpy.column_stack((s.x, s.y)) |
| 1344 | + # run k means clustering algorithm against polygons in gdf |
| 1345 | + kmeans = KMeans(n_clusters=n_clusters, init='k-means++', random_state=5, max_iter=400) |
| 1346 | + y_kmeans = kmeans.fit_predict(X) |
| 1347 | + k = geopandas.pd.DataFrame(y_kmeans, columns=['cluster']) |
| 1348 | + gdf = gdf.join(k) |
| 1349 | + # build polygon for each cluster |
| 1350 | + for n in range(n_clusters): |
| 1351 | + c_gdf = gdf[gdf["cluster"] == n] |
| 1352 | + c_poly = __gdf2poly(c_gdf) |
| 1353 | + clusters.append(c_poly) |
1333 | 1354 | else:
|
1334 |
| - s = gdf.centroid |
1335 |
| - X = numpy.column_stack((s.x, s.y)) |
1336 |
| - # run k means clustering algorithm against polygons in gdf |
1337 |
| - kmeans = KMeans(n_clusters=n_clusters, init='k-means++', random_state=5, max_iter=400) |
1338 |
| - y_kmeans = kmeans.fit_predict(X) |
1339 |
| - k = geopandas.pd.DataFrame(y_kmeans, columns=['cluster']) |
1340 |
| - gdf = gdf.join(k) |
1341 |
| - # build polygon for each cluster |
1342 |
| - for n in range(n_clusters): |
1343 |
| - c_gdf = gdf[gdf["cluster"] == n] |
1344 |
| - c_poly = __gdf2poly(c_gdf) |
1345 |
| - clusters.append(c_poly) |
| 1355 | + raise sliderule.FatalError("Clustering support not enabled; unable to import sklearn package") |
1346 | 1356 |
|
1347 | 1357 | # update timing profiles
|
1348 | 1358 | profiles[toregion.__name__] = time.perf_counter() - tstart
|
|
0 commit comments