Skip to content

Commit

Permalink
fix(array_export.py::export_array_contours): issue with cross contour…
Browse files Browse the repository at this point in the history
… lines/contour level vertices being smashed together in python 3.12
  • Loading branch information
aleaf committed Sep 5, 2024
1 parent 206b146 commit 87206f1
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions mfexport/array_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
import numpy as np
import pandas as pd
import geopandas as gpd
import rasterio
from rasterio import Affine
from shapely.geometry import LineString
Expand Down Expand Up @@ -151,6 +152,7 @@ def export_array_contours(filename, a, modelgrid,
contours = ax.contour(modelgrid.xcellcenters,
modelgrid.ycellcenters,
a, **kwargs)
#plt.savefig('junk.pdf')
plt.close()

if not isinstance(contours, list):
Expand All @@ -162,15 +164,24 @@ def export_array_contours(filename, a, modelgrid,
levels = ctr.levels
for i, c in enumerate(ctr.collections):
paths = c.get_paths()
geoms += [LineString(p.vertices) if len(p) > 1 else LineString() for p in paths]
level += list(np.ones(len(paths)) * levels[i])
for path in paths:
# break the paths up into their components
# (so that different instances of a contour level
# don't connect across other contour levels)
parts = np.split(path.vertices,
np.where(path.codes == 1)[0], axis=0)
parts = [p for p in parts if len(p) > 0]
geoms += [LineString(p) if len(p) > 1 else LineString() for p in parts]
level += list(np.ones(len(parts)) * levels[i])
#geoms += [LineString(p.vertices) if len(p) > 1 else LineString() for p in paths]
#level += list(np.ones(len(paths)) * levels[i])

# convert the dictionary to a recarray
if len(level) == 0:
print('No contours! Try adjusting the levels or interval.')
return
df = pd.DataFrame({'level': level, 'geometry': geoms})
df2shp(df, filename, crs=crs)
gdf = gpd.GeoDataFrame({'level': level, 'geometry': geoms}, crs=crs)
gdf.to_file(filename)
if verbose:
print("array contour export took {:.2f}s".format(time.time() - t0))
return
Expand Down

0 comments on commit 87206f1

Please sign in to comment.