Issue with Python Script not reading geometry length

1069
10
06-18-2021 12:13 PM
RPGIS
by
Regular Contributor

Hi,

 

So I came across a very strange issue, one that I never had issues with previously and then all of sudden I started having issues. The issue is the length field for one of the feature classes isn't reading for some reason. It was able to read in the past but now it doesn't seem to be reading it. I have tested it in a notebook as well as a standalone script.

 

 

import arcpy
import sys
import os

workspace = *

sewerin_fcs = *

ws_split = os.path.split(workspace)

fcs = []

walk = arcpy.da.Walk(workspace, datatype="FeatureClass")

for dirpath, dirnames, filenames in walk:
        for filename in filenames:
                #Get feature class name
                fcsname = os.path.basename(filename)
                name = os.path.splitext(fcsname)
                y = name[1].lstrip('.')

                if y == sewerin_fcs:
                        fcs.append(os.path.join(dirpath, filename))


for fc in fcs:
        fc_fields = []

        
        important_fields = ['OBJECTID', 'SHAPE@LENGTH']

        allfields = arcpy.ListFields(fc)
        for field in allfields:
                #print field.name
                if field.name in important_fields:
                        print (field.name)

 

 

 

When the script finishes running, this is the printed result.

Printed Result.JPG standalone script.

Jupyter Notebook Result.JPGJupyter Notebook script

It appears that the database in which these fields are sourced is the issue. When I checked all of the feature classes in the database that also have this field, the script wasn't working. I don't know if has something to do with field visibility or a setting within the database itself.

The script creates a detailed report of all the important feature classes within multiple databases within the default sde databases and compares those feature classes to the multiple databases within our production database. I am not sure how this issue can be resolved, but it is something that I don't quite know on how to trouble shoot.

I would greatly appreciate it if someone could assist me with this issue.

 

0 Kudos
10 Replies
forestknutsen1
MVP Regular Contributor

What database are you on?

We are on oracle. For this sort of stuff, I use SQL embedded in python (or sometimes pure SQL) It is much faster than going through arcpy.

0 Kudos
RPGIS
by
Regular Contributor

Thanks forestknutsen,

I haven't tried using sql from python and I don't quite know the what correct module is needed to access the sde database or if there is already an existing module that is installed in python that will allow me to access the sde database.

 

Do you know if the module is already there when python is installed or is there another module that you are using to access the database? I have been looking and I think there is an already installed module, but I am not sure if the module that is installed has full SQL querying capabilities or if it is limited.

If it isn't too much to ask, would you mind letting me know. I am trying to generate several reports simultaneously and for the most part, the script works but there is the issue of this single dataset that for some reason is prohibiting the length field from being read for some of the feature classes.

0 Kudos
forestknutsen1
MVP Regular Contributor

No problem. I use cx_oracle. 

https://cx-oracle.readthedocs.io/en/latest/

of course, it only works on sde deployments with oracle as the base technology. You have to install it (it does come as part of the standard library).

There is also ArcSDESQLExecute:

https://desktop.arcgis.com/en/arcmap/10.6/analyze/arcpy-classes/arcsdesqlexecute.htm

It does not require an install (part of arcpy). And it does not care what your dbms technology is. But it is not as nice to use as cx_oracle. So, I almost never use it. Let me know if you are on oracle and I will post a code sample.

RPGIS
by
Regular Contributor

Thanks forestknutsen1.

We use SQL databases so anything Oracle related I wouldn't be able to use. Thank you though for the offer, I will let you know if we end up utilizing something related to Oracle where that kind of script would come in handy.

0 Kudos
DanPatterson
MVP Esteemed Contributor

case sensitive?

Shape_Length  in my gdb featureclass

You are safer reading the shape field directly and getting the geometry from it using the SHAPE@ token


... sort of retired...
RPGIS
by
Regular Contributor

Thanks Dan,

Does the shape field also read the length field as well? I just need the length field for a report that the script prints out with details on the specified feature classes with their total combined lengths for each record and print a text file of those total lengths.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Robert, I was initially pointing out that my shape length field is Shape_Length and not  SHAPE@LENGTH  which is token to read the shape field and return the length.... it is different, it is reading the actual shapefield and returning a property.

If you are reading actual fields, use their field names, if you want to read the tokens for the fields, then use those ( OID@ and SHAPE@LENGTH for file gdb )


... sort of retired...
0 Kudos
RPGIS
by
Regular Contributor

Thanks Dan,

I also tried using the field name as well and the result was still the same. So I tried using the shape@length for the featureclass(s) to see if the result would be different but the result didn't change. I am under the impression that the issue perhaps lies in the database itself and its permissions, but one of our dbas is looking into it.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Robert, my examples are for file geodatabases.


... sort of retired...
0 Kudos