Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(shapefile_utils): warn if fieldname truncated per 10 char limit #2003

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions flopy/export/shapefile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
import warnings
from pathlib import Path
from typing import Optional, Union
from typing import List, Optional, Union
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -471,20 +471,28 @@ def shape_attr_name(name, length=6, keep_layer=False):
return n


def enforce_10ch_limit(names):
def enforce_10ch_limit(names: List[str], warnings: bool = True) -> List[str]:
"""Enforce 10 character limit for fieldnames.
Add suffix for duplicate names starting at 0.

Parameters
----------
names : list of strings
warnings : whether to warn if names are truncated

Returns
-------
list
list of unique strings of len <= 10.
"""
names = [n[:5] + n[-4:] + "_" if len(n) > 10 else n for n in names]

def truncate(s):
name = s[:5] + s[-4:] + "_"
if warnings:
warn(f"Truncating shapefile fieldname {s} to {name}")
return name

names = [truncate(n) if len(n) > 10 else n for n in names]
dups = {x: names.count(x) for x in names}
suffix = {n: list(range(cnt)) for n, cnt in dups.items() if cnt > 1}
for i, n in enumerate(names):
Expand Down