|
POST
|
The dictionary's value for each key is supposed to be a list with two items: length, shape. The second item is the shape. So the failure may be happening on a specific record of the dictionary. Set a counter and print it in the for loop to determine if the failure is happening for the first record or if it is happening on a specific record of the dictionary. If it is happening on the first record then publish the print you get of the dictionary. If it happens on a specific record, isolate which record fails and print its contents. This is standard debugging and you should assume nothing. The Split line at point is capable of producing features with Null geometry and this could cause a failure of the logic I used to populate the dictionary if that created an exceptional case I did not account for.
... View more
02-16-2017
12:47 AM
|
1
|
2
|
1212
|
|
POST
|
You need the length value and geometry in the value of the dictionary. You can drop the length field from the updatecursor, but to extract just the geometry from the dictionary you would need to change the line that reads: row[2] = valueDict[PROPNUM] to: row[1] = valueDict[PROPNUM][1] The code should be: import arcpy
arcpy.env.overwriteOutput = True
sourceFC = r'\\qep\dnshare\GIS\Haynesville\WorkspaceGDBs\HaynesvilleRecovery.gdb\sticksRF_split'
targetFC = r'\\qep\dnshare\GIS\Haynesville\WorkspaceGDBs\HaynesvilleRecovery.gdb\sticksRF'
sourceFields = ["PROPNUM_GIS","Shape_Length","SHAPE@"]
print sourceFields
print "Using search cursor to create dictionary of features with greatest Shape_Length value for each unique value in PROPNUM_GIS field"
valueDict = {}
with arcpy.da.SearchCursor(sourceFC, sourceFields) as searchRows:
for searchRow in searchRows:
keyValue = searchRow[0] # PROPNUM_GIS is the key value searchRow?
if not keyValue in valueDict:
valueDict[keyValue] = [searchRow[1:]]
elif valueDict[keyValue][0] < searchRow[1]:
valueDict[keyValue] = [searchRow[1:]]
print valueDict # Use print to make sure dictionary is right. Should be a list of unique PROPNUMs and longest lengths. Looks good, 2/14/17.
with arcpy.da.UpdateCursor(targetFC, ["PROPNUM_GIS","SHAPE@"]) as uCur:
for row in uCur:
PROPNUM = row[0]
if PROPNUM in valueDict:
row[1] = valueDict[PROPNUM][1]
uCur.updateRow(row)
... View more
02-15-2017
05:04 PM
|
1
|
4
|
3098
|
|
POST
|
Nicole: Line 15 adds the station field as the first field in the list, because this is field holding the unique dictionary key value used match records between each table per your description of what you wanted to do. The field list includes the station twice, once for the dictionary key and once for the value of the field for a given row, which is important later when the station field has to be in the position it exists relative to other fields to hold all the values of a row object that is passed to the insert cursor. Line 19 reads the records of the search table that contains the table names. Lines 18-23 - you are correct that the dictionary is adding each station only the first time it is encountered to the dictionary and the key value is the concatenation of the two fields that make up your table names, per your description of what you had set up. For the modification of the code, I did not test using the next method. Go back to using the for loop and break out of it once you have created a feature for the table. I.e.: for station in sorted(stationDict):
table_name = stationDict[station]
with arcpy.da.SearchCursor(table_name, pfieldnames) as pCursor:
for pRow in pCursor:
stationpointDict[station] = pRow
pfieldsvalues = pRow
ShapeVal = [pfieldsvalues[4], pfieldsvalues[5]]
fillValues = [ShapeVal] + list(pfieldsvalues)
print(fillValues)
newPoint.insertRow(fillValues)
print("point " + str(station) + " added to All_points")
break
del pCursor
... View more
02-15-2017
07:22 AM
|
2
|
0
|
923
|
|
POST
|
One of the possible causes could be the order of the attributes, since the insert cursor data has to match the field order exactly. If you eliminate everything other than the PROPNUM and shape fields that should avoid that problem. Do not revise the if statements that populate the first dictionary. They are both necessary. The first adds the PROPNUM key if it is not already in the dictionary and the second replaces the value of the existing PROPNUM in the dictionary if the length field is greater than the value currently stored. The PROPNUM and shape_length fields need to come before the geometry in the field list for that dictionary. The keys are not mismatched. The PROP NUM is the key value, the length is the first value in the list object and the row tuple is the second item in the list. When the row is being populated the row tuple is being pass to the insertcursor. It is possible the shape field is not considered editable and needed to be added back to the field list, which is why you would not see any lines. I was not sure if the shape field is included or not by the editable parameter. Either way the shape field and all other fields have to be in the exact order that they shows up in the output table relative to all of the other fields for the insertcursor to work.
... View more
02-13-2017
03:32 PM
|
1
|
1
|
3098
|
|
POST
|
It is possible that more than two features could have the same PROPNUM_GIS value if you are processing multiple point splits during the same geoprocessing operation, so I will assume that you always want the longest segment regardless of how many splits occurred on a given line. The code below should do what you want: import arcpy
sourceFC = "C:/Path/MyFeatureClass"
field_names = []
fields = arcpy.ListFields(sourceFC)
for field in fields:
if field.editable:
field_names.append(field.name)
all_editable_fields = field_names
sourceFieldsList = ["PROPNUM_GIS", "Shape_Length"] + all_editable_fields
valueDict = {}
with arcpy.da.SearchCursor(sourceFC, sourceFieldsList) as searchRows:
for searchRow in searchRows:
keyValue = searchRow[0]
if not keyValue in valueDict:
valueDict[keyValue] = [searchRow[1:]]
elif valueDict[keyValue][0] < searchRow[1]:
valueDict[keyValue] = [searchRow[1:]]
arcpy.CreateFeatureclass_management("C:/Path", "NewFeatureClass", "POLYLINE", "C:/Path/MyFeatureClass", "DISABLED", "DISABLED", "C:/Path/MyFeatureClass")
lines = arcpy.da.InsertCursor("C:/Path/NewFeatureClass", all_editable_fields)
for propnum_gis in valueDict:
fieldValues = valueDict[propnum_gis][1:]
lines.insertRow(fieldValues)
... View more
02-13-2017
12:55 PM
|
1
|
3
|
3098
|
|
POST
|
Your code is using far too many cursors. It is faster to blast through a complete table than it is to use expressions to limit the result most of the time, especially since you have to blast through the whole table to build your expression for the second cursor on that table. Don't bother trying to find the first records, just use it when going through the whole table and it meet your criteria and keep track of the fact you have already used it or break out of the loop. Anyway, I think the code below does what you are trying to do and is far less confusing. I could make the code more efficient if I knew exactly what each input table contained. I did not assume that the table_name tables contained data for only one station. If that is actually the case then the second dictionary, the last for loop and the last two if clauses can be replaced with pfieldvalues = next(pCursor). None of this may solve your original problem, since there are too many points of failure that could have happened in your prior script or in your input tables. Are you positive that all fields of the Test1 table correctly point to all of the tables created by your previous script? Did you verify your previous script created all of the expected tables? Are you sure there are no misspellings of any of the input table names or field values? If the script I wrote has the same failure as every other script you have tried I would say the cause of the failure is in the inputs to this script. import arcpy
import os
#Workspace settings
arcpy.env.workspace=r"C:\data\2017\LakeKivu\Kivu_python.gdb"
arcpy.env.overwriteOutput= True
#input file
table = r"C:\data\2017\LakeKivu\Kivu_python.gdb\Test1"
points=r"C:\data\2017\LakeKivu\Kivu_python.gdb\Campaigns\All_points"
dsc = arcpy.Describe(table)
fields = dsc.fields
# List all field names except the OID field
fieldnames = ["Station"] + [field.name for field in fields if field.name != dsc.OIDFieldName]
# create a dictionary of stations
stationDict = {}
with arcpy.da.SearchCursor(table, fieldnames) as cursor:
for row in cursor:
station = row[0]
if not station in stationDict:
stationDict[station] = str(row[1])+"_"+str(row[2])
pfieldnames=['Cruise','Station', 'Type', 'Date', 'Longitude', 'Latitude','UPI']
pfields=arcpy.ListFields(points)
fillnames=[pfield.name for pfield in pfields if pfield.name != dsc.OIDFieldName] #names for point feature class
newPoint=arcpy.da.InsertCursor(points, fillnames)
stationpointDict = {}
for station in sorted(stationDict):
table_name = stationDict[station]
with arcpy.da.SearchCursor(table_name, pfieldnames) as pCursor:
for pRow in pCursor:
if station == pRow[1]:
if not station in stationpointDict:
stationpointDict[station] = pRow
pfieldsvalues = pRow
ShapeVal = [pfieldsvalues[4], pfieldsvalues[5]]
fillValues = [ShapeVal] + list(pfieldsvalues)
print(fillValues)
newPoint.insertRow(fillValues)
print("point " + str(station) + " added to All_points")
... View more
02-13-2017
11:30 AM
|
2
|
5
|
7072
|
|
POST
|
As Dan, Joshua and others have pointed out, the creation of an InsertCursor should never happen within a loop. You only want to declare the InsertCursor once for any given table/fc (typically before any loops that involve it). Only the insertion of rows should be done within a loop: points=r"C:\data\2017\LakeKivu\Kivu_python.gdb\Campaigns\All_points"
pfields=arcpy.ListFields(points)
fillnames=[pfield.name for pfield in pfields if pfield.name != dsc.OIDFieldName] #names for point feature class
newPoint=arcpy.da.InsertCursor(points, fillnames)
for profile in profile_nrs:
# get a list of fillValues using some sort of method
newPoint.insertRow(fillValues)
... View more
02-13-2017
10:07 AM
|
1
|
0
|
3123
|
|
POST
|
I find that it very useful to calculate the X and Y coordinates of both ends of my network lines into four separate double fields stored in the line attributes (FROM_X, FROM_Y, TO_X, TO_Y). You can easily calculate the values for these fields using the geometry calculator. I automatically maintain these fields whenever I edit the line geometry using the Attribute Assistant X_Coordinate and Y_Coordinate methods with the S (start) or E (end) value options (only works for geodatabases, not shapefiles). As Chris Donohue, GISP, said, if your network is highly segmented and you want the overall trend of a complete road you would need to use the Dissolve tool, Create Route tool or some other aggregating tool first to get those trends. Once you have these coordinate fields it is relatively straight forward to select the lines that are trending more north/south than east/west provided you are using a projected coordinate system (which expresses coordinates in linear units like meters or feet) and your lines are not very sinuous (curvy) or multipart lines with large gaps or self intersections. You can do a selection like: To find north trending lines: ABS(TO_X - FROM_X) < ABS(TO_Y - FROM_Y) AND FROM_Y < TO_Y To find south trending lines: ABS(TO_X - FROM_X) < ABS(TO_Y - FROM_Y) AND FROM_Y > TO_Y If you are concerned that some of your roads are very curvy you can separate those roads as long as you have a length field (for geodatabases this is built-in, but for shapefiles you have to calculate this using the geometry calculator or a python calculation). Assuming the linear units of the length and coordinates are the same kind, you can exclude sinuous roads by adding this clause to the selection expressions above (this SQL works for a File Geodatabase and shapefile and may not work for Personal Geodatabases and enterprise geodatabases like SQL Server and Oracle): AND Shape_Length / Power((Power(From_X - To_X, 2) + Power(From_Y - To_Y, 2)), 0.5) <= 1.5 Change the above expression to > 1.5 to find roads that are considered meandering. You can adjust the sinuosity factor number (1.5) to higher numbers to tolerate more curviness, or to lower numbers to tolerate less curviness. A perfectly straight singlepart line has a sinuosity factor of 1. Closed lines (lines that start and end at the same point) have an infinite or divide by zero error sinuosity factor. Multipart lines with large gaps between parts can have a sinuosity factor that is less than 1.
... View more
02-10-2017
01:22 PM
|
1
|
1
|
3198
|
|
BLOG
|
I have created an improved version of my tool for ArcMap 10.3 (Field Match Tools.pyt.zip). It now has options that allow string field matches to be case sensitive or case insensitive and that will trim whitespace from strings on Both ends, the Left end, the Right end or None of the ends. The names of the options controlling the type of relationship the single key field values will represent have been shortened to make the tool easier to use in Python scripts. I have also included my tool for appending selected records from one layer or table view to itself or another. I have only updated the ArcMap 10.3 version of the tool, because that tool interface is much easier to use and I have only been using ArcMap 10.3 for the last several years.
... View more
02-09-2017
09:52 AM
|
0
|
0
|
2400
|
|
POST
|
In that case, if you have any further questions about SQL expressions, you have to tell us up front which Enterprise database you are using. For most sophisticated query expressions the answer you need will completely depend on the enterprise database you are using.
... View more
02-08-2017
04:25 PM
|
1
|
1
|
2404
|
|
POST
|
What is your data source type? CURRENT_DATE works with File Geodatabases (I tried the SQL on my own data before posting it). It should also work for a shapefile. It would not work with a Personal Geodatabase and probably won't work with any Enterprise database (SQL Server, Oracle, etc.).
... View more
02-08-2017
04:01 PM
|
1
|
3
|
2404
|
|
POST
|
If this is a file geodatabase the expression would be: CURRENT_DATE - COMPLETED < 14 OR (COMPLETED IS NULL AND CURRENT_DATE - CREATED < 14) The features would disappear if they were completed 14 days before today or had no completed date and were created 14 days before today. For example, January 25, 2017 would not appear on February 8, 2017, but January 26, 2017 would appear.
... View more
02-08-2017
03:42 PM
|
1
|
5
|
2404
|
|
POST
|
The Like and wildcard expression is the easiest to use if you want to match a given portion of a set string. However, there is a way to write code that is like your original approach, but the expression is not LEFT, it is SUBSTRING (at least for file geodatabases and shapefiles). The basic syntax is: SUBSTRING(string_exp FROM start FOR length) So for your problem it would be: SUBSTRING(FEATURE_TYPE FROM 1 FOR 1) = 'Y' Your title suggested you wanted to find any string that began with X, Y or Z. That could be done with this expression: SUBSTRING(FEATURE_TYPE FROM 1 FOR 1) IN ('X', 'Y', 'Z')
... View more
02-07-2017
07:39 AM
|
2
|
0
|
5670
|
|
POST
|
Creating a new layer in ArcMap by using copy/paste, saving a layer file or creating a selection layer only creates a new view of the original data, not a new copy of the original data. This is useful for changing symbology, labeling and other presentation properties of the copied layer within a map of a single data source. It is not useful for duplicating a data source so that the original data source is untouched by editing. To create a copy of the original data source you need to export the layer (right-click the layer->Data->Export), copy/paste the feature class in ArcCatalog, or use a geoprocessing tool that copies a feature class like the Feature Class to Feature Class tool.
... View more
01-31-2017
07:21 AM
|
2
|
1
|
989
|
|
POST
|
LR is screwed up for offsets. Lines offset in the opposite direction of the points when you use the same advanced setting for the side of offset. Change the points to be the opposite advanced setting for the side to get it to behave better. I have not seen any skew using my data that looks anything like the amount of skew shown on your horizontal line on the right.
... View more
12-27-2016
10:51 AM
|
0
|
0
|
1398
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 03-24-2026 11:37 PM | |
| 1 | 03-24-2026 08:01 PM | |
| 6 | 02-23-2026 08:34 AM | |
| 1 | 03-31-2025 03:25 PM | |
| 1 | 03-28-2025 06:54 PM |
| Online Status |
Offline
|
| Date Last Visited |
a week ago
|