1

I want to use Python in ArcMap 10.5 Python window to give me the longest string in a field. I need to trim down some shapefile (they have to be shapefiles) file sizes. I want to reduce field lengths where possible. I am also going to run simplify tool. I am asking for Python help in this post, not ways to decrease shapefile size.

I don't understand how to specify which shapefile in my workspace for the script to run upon. Here is the script, most of it copy and pasted from another StackExchange post.

import arcpy

arcpy.env.workspace = "W:\WildCAD6\GIS"

#HOW DO I SET THE SHAPEFILE THAT I WILL NEXT CHOOSE FIELD FROM?

def longestWord(!FIELD!): #IS THIS WHERE I SET THE FIELD? 
    count = 0    #You set the count to 0
    for i in !FIELD!: # Go through the whole list
        if len(i) > count: #Checking for the longest word(string)
            count = len(i)
            word = i
    return ("the longest string is " + word)
8
  • are you open to a non-arcpy solution?
    – Paul H
    Commented Sep 27, 2019 at 20:23
  • Sure, that is fine. Was hoping to learn a bit more Python but what in the non-arcpy solution?
    – sparky
    Commented Sep 27, 2019 at 20:25
  • you could use numpy or geopandas to do this very easily.
    – Paul H
    Commented Sep 27, 2019 at 20:25
  • Are you able to elaborate more? I have no idea how to go about either of those.
    – sparky
    Commented Sep 27, 2019 at 20:26
  • 2
    Reducing dataset storage is a trivial task if you simply abandon the shapefile format. File geodatabase tables use a fraction of the storage of shapefiles, and support additional field types, and numeric nulls, etc. The time spent flailing about, trying to make the shapefiles less prolifigate could instead be used in optimizing the coordinate reference parameters, to shave 20-40% more off the geometry savings.
    – Vince
    Commented Sep 27, 2019 at 21:23

2 Answers 2

2

To do this I would just use ArcPy, a SearchCursor from its Data Access (arcpy.da) module and Python list comprehension.

Running this code:

import arcpy

shp = r"C:\Temp\ne_10m_admin_0_countries.shp"

for fld in arcpy.ListFields(shp,"","String"):
    longestString = max([len(row[0]) for row in arcpy.da.SearchCursor(shp,[fld.name])])
    print("Field name: {0} Max. string length: {1}".format(fld.name,longestString))

produces output that begins like this:

========================== RESTART: C:\Temp\test.py ==========================
Field name: featurecla Max. string length: 15
Field name: SOVEREIGNT Max. string length: 32
Field name: SOV_A3 Max. string length: 3
Field name: TYPE Max. string length: 17
Field name: ADMIN Max. string length: 36
Field name: ADM0_A3 Max. string length: 3
Field name: GEOUNIT Max. string length: 36
Field name: GU_A3 Max. string length: 3
Field name: SUBUNIT Max. string length: 36
Field name: SU_A3 Max. string length: 3
Field name: NAME Max. string length: 25
Field name: NAME_LONG Max. string length: 36
0
1

Here's how I would do this with geopandas:

import geopandas

def longest_string_gpd(gisfile, field):
    return (
        geopandas.read_file(gisfile)
            .assign(length=lambda df: df['field'].map(len))
            .loc[lambda df: df[field].idxmax(), field]
    )

Here's how I would do this with arcpy/numpy:

import arcpy
import numpy

def longest_string(gisfile, field):
    field_names = numpy.unique([f.name for f in arcpy.ListFields(gisfile)])
    array = arcpy.da.FeatureClassToNumPyArray(in_table=gisfile, field_names=fields)
    array['length'] = [len(x) for x in array[field]]
    return array[numpy.argmax(array['length'])][field]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.