|
POST
|
I have a basic python code that is intended to take the contents of a File GDB and ZIP them (not that actual .gdb itself though). When I test the code it says it's all good. When I run the code however, it goes straight to "Exit Code 0" and nothing happens. No new ZIP file, nothing. Any ideas where I'm going wrong? import os
import shutil
import zipfile
# Creates a zip file containing the input shapefile
# inFileGDB: Full path to FileGDB folder to be zipped
# Delete: Set to True to delete fileGDB files after zip
# Creates a zip file containing the contents of a filegeodatabase
def Zipfgdb(inFileGDB = r'\\my\data\pathway\MyData.gdb', Delete = 'False'):
print inFileGDB + " : Delete original files = " + Delete
#Directory of file geodatabase
inLocation = os.path.dirname (inFileGDB)
print "inLocation: " + inLocation
#Base name of shapefile
inName = os.path.basename (os.path.splitext(inFileGDB)[0])
print "inName: " + inName
#Create the zipfile name
zipfl = os.path.join (inLocation, inName + ".zip")
print "New ZIP file: " + zipfl
#Create zipfile object
ZIP = zipfile.ZipFile (zipfl, "w")
print "ZIP file created"
#Iterate files in shapefile directory
for fl in os.listdir (inFileGDB):
#Get full path of file
inFile = os.path.join (inFileGDB, fl)
#Add file to zipfile. exclude any lock files
if os.path.splitext(fl)[1][1:] <> 'lock':
ZIP.write(inFile,fl)
print "files added to ZIP file"
#Delete filegeodatabase if indicated
if Delete == True:
shutil.rmtree(inFileGDB)
print "Original file deleted"
#Close zipfile object
ZIP.close()
#Return zipfile full path
return zipfl
... View more
03-06-2019
09:04 PM
|
0
|
4
|
2743
|
|
POST
|
Can someone please explain to me what the perceived benefits of using Contingent Values are instead of using SubTypes? Contingent values—Geodatabases | ArcGIS Desktop I understand that by using Contingent values you essentially have 2 lists (Domains), with the 2nd list being filtered by the selection from the first list. This compares to Subtypes where the selection of the first list would determine which secondary list another field looks to (multiple lists to pick from). Also, what I can't find anywhere is if I setup data using Contingent Values and publish the layer as a service to ArcOnline, will the Contingent values be honoured (and be editable via the Data tab on the service overview page as you can with Domains)?
... View more
02-04-2019
07:00 PM
|
3
|
6
|
7121
|
|
POST
|
Thanks for the tip. Linkkey should be unique as well so this shouldn't be an issue for us. Good to know how to deal with it if this were the case though.
... View more
11-25-2018
04:33 PM
|
0
|
1
|
4641
|
|
POST
|
I have 2 dictionaries (A and B). A is a new list of keys which is being checked against B. If a key is in B but not A (is an old value), I'm attempting to get the first value from the associated key in the dictionary (in this case an OBJECTID). The issues I'm having are with line 32. As far as I can tell, this should iterate through all the keys in B, see if they're in A, and if not, write their value to a text file. As it happens, the text file is just getting the full list of old values (dictionary B) instead of only the ones not in both feature classes/dictionaries. Where are we going wrong? I've attached the output text file import arcpy
# Set environmental variables
arcpy.env.overwriteOutput = True
new = r"W:\Mapping\ArcGIS Online\ArcGIS Online Features.gdb\Collector_patch" #new feature class
old = r"W:\Mapping\ArcGIS Online\Upload.gdb\AGOL_Export" #old feature class
field = 'LinkKey', 'OBJECTID' #feature class fields to compare & return
fout = r"W:\Mapping\ArcGIS Online\FPC Plantations Features to Delete.txt" #output text file
a = {}
b = {}
#The old feature class is loaded into dictionary b
with arcpy.da.SearchCursor(old, (field)) as rows:
for row in rows:
if row[0] not in b:
b[row[0]] = [row[1]]
#The new feature class is loaded into dictionary a
with arcpy.da.SearchCursor(new, (field)) as rows:
for row in rows:
if row[0] not in a:
a[row[0]] = [row[1]]
#Below compares the dictionaries and extracts features with old Linkkeys"
fo = open(fout, "w")
for k, v in b.iteritems():
if k not in a.items()[0]:
print k
print v
fo.write(str(v[0]) + ',')
print "Linkkeys not in new feature class exported"
fo.close()
... View more
11-23-2018
12:13 AM
|
1
|
5
|
4864
|
|
POST
|
I think it must have been the pathway. It was meant to be a complete path but I think the "gis" part was an alternative to the actual path of "bun-gis-01". I tweaked it and it worked so hopefully it keeps working. Thanks!
... View more
10-10-2018
12:38 AM
|
0
|
0
|
1571
|
|
POST
|
I've got a pretty simple script that looks for the latest/newest file in a folder and copies it into a new location. The script works fine when run through a python GUI, but when run as a part of an Arc model, fails. Can someone translate the error for me? I don't understand why this is happening? import glob
import os
import shutil
import time
date = time.strftime("%Y-%m-%d")
dst = "//10.253.2.161/cddp/Bunbury Use Only/GeoMasterPatchArchives/" + date
list_of_files = glob.glob('//gis/Geomaster/*.bak') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
shutil.copy(latest_file, dst)
print latest_file + " copied to backup folder"
... View more
10-09-2018
07:28 PM
|
0
|
2
|
1674
|
|
POST
|
Where did you change these settings? I can't see anything in our Organisations AGOL account on the web.
... View more
09-20-2018
05:23 PM
|
0
|
1
|
3477
|
|
POST
|
I found a solution. I had a niggling feeling that it was the comparisons between original and current extent that was not behaving the way I expected. I originally thought this would be the best indication of the map having moved/changed based on the features in the layers. After seeing the scale print statements and how fine they were (i.e. oldscale = 404659.559547) I figured it would be highly unlikely these would ever occur twice. Thus, I changed the code from comparing original and current extent to scales and now it works as expected. Can't thank you enough for your help with this!
... View more
09-18-2018
07:08 PM
|
0
|
0
|
1265
|
|
POST
|
Thanks for your help. Glad we at least got the index issues sorted! I'll have to look into python 3. Anything in particular here that won't talk in the new version? I'm guessing the syntax has changed?
... View more
09-18-2018
07:03 PM
|
0
|
1
|
1265
|
|
POST
|
That makes sense and works (insert face plant here). The part that I must have been getting hung up on though is the desire for this to be returning a typical scale concentrated on a small area (i.e. 1:20,000), meaning that the logic I've used to get the map to focus on the grouped features isn't working as expected. Fore example. Before running the script, the map is focused on a selection of polygons in one location (EIG > 0 and EIGA = 0). I run this script to change the definition query to look at a different selection of polygons in a different location (where EIG = 0 and EIGA > 0). The code should examine the if query at Line 43 as False and skip to the next ELIF (Line 47) resulting in an unchanged dataframe extent (at this point). Next it should examine the if EIGA > 0 (Line 51) as TRUE then set the current extent variable. Line 54 sees the current extent compared to the original extent (should result in TRUE) and proceed to Line 55. It would apply the layer extent to the dataframe and step up to the next in the list. That should be the end of it but for some reason it is progressing through to the elif at Line 61 and finding that TRUE. I would expect this to be FALSE because the current_ext and orig_ext variables are unchanged (to my knowledge) despite the dataframe extent being updated so should result in no further action being taken. What actually seems to be happening is the code is running the ELIF (Line 61) as TRUE and comparing the min/max values from the old extent and the new extent resulting in a huge scale. This should only be getting run if there are new features in both layers which will be similarly geolocated. This does work as expected when both layers > 0.
... View more
09-18-2018
06:40 PM
|
0
|
4
|
7829
|
|
POST
|
I've eliminated a big chunk of code that doesn't apply to this MXD so the lines are different from the original but I've been trying to factor that into my post/responses. Still looks correct to me though here? I added in the print statements and this is the result when running a query where EIG = 0 and EIGA > 0 (sorry if I'm formatting this wrong - trying to break it up into the info/error sections). Executing: UpdateDefinitionQueries "Lake Muir 2" LM2 # # MLM7AFC
Start Time: Wed Sep 19 08:48:54 2018
Running script UpdateDefinitionQueries...
Entered In GeoMaster definition query updated
Entered In GeoMaster Archive definition query updated
scalelist: [5000, 7500, 10000, 12500, 20000, 25000, 30000, 40000, 50000, 60000, 70000, 75000, 80000, 90000, 100000]
old scale: 404659.559547
Failed script UpdateDefinitionQueries...
Traceback (most recent call last):
File "W:\Mapping\ToolboxPythonCodes\SCRIPTS\Update Definition Queries.py", line 78, in <module>
newscale = scalelist[bisect(scalelist, oldscale)] # bisect old scale to get new scale
IndexError: list index out of range
Failed to execute (UpdateDefinitionQueries). Failed at Wed Sep 19 08:49:05 2018 (Elapsed Time: 10.25 seconds)
... View more
09-18-2018
05:54 PM
|
0
|
6
|
7829
|
|
POST
|
Thanks Vince for the tip. I've updated the original code snippet so hopefully I have that correct now. This still isn't helping the "list index out of range" error though at line 78.
... View more
09-18-2018
05:35 PM
|
0
|
8
|
7829
|
|
POST
|
It was intended to just skip that section of code if it didn't meet the count criteria (no features so no need to apply the layer extent to the data frame extent). I assumed that if it didn't meet that criteria it would just move on to the next valid bit of code instead or needing an ELIF. Have I figured that incorrectly?
... View more
09-17-2018
04:55 PM
|
0
|
10
|
7829
|
|
POST
|
The code below seems to function correctly most of the time except when running through a situation where line 43 is False (no extent updates based on this layer) and Line 51 is True (1+ feature in this layer), meaning Line 54 is also True (extent set at Line 22 should still match extent at Line 52). What I'm not understanding is 2 things: 1. Why is it running through the elif at Line 61 when the condition should be False (as per Line 54 being True) 2. Why when it does run through the elif at Line 61 I get the below Trackback error about the list Index being out of range when it works elsewhere in the same manner? Hope that all makes sense! import arcpy
from arcpy import env, mapping
from bisect import bisect
#input parameters;
Plantation = arcpy.GetParameter(0)
ForestID = arcpy.GetParameter(1)
CommonName = arcpy.GetParameter(2)
PYear = arcpy.GetParameter(3)
OpCode = arcpy.GetParameter(4)
#variables;
title1 = Plantation + " PLANTATION"
title2 = Plantation + "\r\nPLANTATION"
title3 = Plantation + " (" + CommonName + ")"
title4 = Plantation + " P" + PYear + " AREA STATEMENT"
mxd = mapping.MapDocument("CURRENT")
elements = mapping.ListLayoutElements(mxd)
layers = mapping.ListLayers(mxd)
HTL_layer_list = ["Entered In GeoMaster", "Entered In GeoMaster Archive"]
dflist = arcpy.mapping.ListDataFrames(mxd, "")
orig_ext = dflist[0].extent
new_exp = "PLANTATION = '" + Plantation + "'"
new_sur_exp = "PLANTATION <> '" + Plantation + "'"
as_new_exp = "EnteredInGeoMaster IS NOT NULL AND Plantation = '" + Plantation + "'"
opcode_exp1 = "Entered_In_GeoMaster IS NOT NULL AND Ops_Code = '" + OpCode + "'"
opcode_exp2 = "Ops_Code = '" + OpCode + "'"
scalelist = [5000, 7500, 10000, 12500, 20000, 25000, 30000, 40000, 50000, 60000, 70000, 75000, 80000, 90000, 100000]
if mxd.title <> ("Cutting Returns Entered in GeoMaster"):
###other code here###
elif mxd.title == ("Cutting Returns Entered in GeoMaster"):
mxd.activeView = "Page_Layout"
for lyr in layers:
if lyr.name == "Entered In GeoMaster":
lyr.definitionQuery = opcode_exp1
arcpy.AddMessage(lyr.name + " definition query updated")
EIGfeature_count = arcpy.GetCount_management(lyr)[0]
if EIGfeature_count > 0:
ext = lyr.getExtent() #gets the new extent of the Entered In GeoMaster layer
dflist[0].extent = ext #applies the Entered In GeoMaster layer extent to the dataframe
elif lyr.name == "Entered In GeoMaster Archive":
lyr.definitionQuery = opcode_exp2
arcpy.AddMessage(lyr.name + " definition query updated")
EIGAfeature_count = arcpy.GetCount_management(lyr)[0]
if EIGAfeature_count > 0:
current_ext = dflist[0].extent
if current_ext == orig_ext:
ext = lyr.getExtent() #gets the new extent of the Entered In GeoMaster layer
dflist[0].extent = ext #applies the Entered In GeoMaster layer extent to the dataframe
oldscale = dflist[0].scale # Get scale of dataframe
newscale = scalelist[bisect(scalelist, oldscale)] # bisect old scale to get new scale
dflist[0].scale = newscale # Apply new scale to dataframe
elif current_ext <> orig_ext:
# get current map extent
xmin, xmax = dflist[0].extent.XMin, dflist[0].extent.XMax
ymin, ymax = dflist[0].extent.YMin, dflist[0].extent.YMax
# loop through def query layer extents and create one extent to fit them all
lyr_ext = lyr.getExtent()
if lyr_ext.XMin < xmin:
xmin = lyr_ext.XMin
if lyr_ext.YMin < ymin:
ymin = lyr_ext.YMin
if lyr_ext.XMax > xmax:
xmax = lyr_ext.XMax
if lyr_ext.YMax > ymax:
ymax = lyr_ext.YMax
# set df extent to new extent
dflist[0].extent = arcpy.Extent(xmin, ymin, xmax, ymax)
oldscale = dflist[0].scale # Get scale of dataframe
newscale = scalelist[bisect(scalelist, oldscale)] # bisect old scale to get new scale
dflist[0].scale = newscale # Apply new scale to dataframe
elif lyr.name == "PlantationsHarvestPlan_OperationsPlanner_WA":
lyr.definitionQuery = new_exp
arcpy.AddMessage(lyr.name + " definition query updated") Traceback (most recent call last):
File "W:\Mapping\ToolboxPythonCodes\SCRIPTS\Update Definition Queries.py", line 78, in <module>
newscale = scalelist[bisect(scalelist, oldscale)] # bisect old scale to get new scale
IndexError: list index out of range For reference, the aim here is to updated the definition queries on 2 layers in an open MXD, check for features in each layer and zoom the extent to view all the features in both layers before stepping the scale up (using bisect) to the next round scale in the list.
... View more
09-16-2018
11:53 PM
|
0
|
12
|
12487
|
|
POST
|
Thanks. I'm going to try both solutions and I think both will likely work form the logic I'm reading.
... View more
09-16-2018
05:13 PM
|
0
|
0
|
3715
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 08-11-2025 07:24 PM | |
| 1 | 3 weeks ago | |
| 4 | 3 weeks ago | |
| 6 | 3 weeks ago | |
| 2 | 3 weeks ago |
| Online Status |
Offline
|
| Date Last Visited |
Tuesday
|