|
POST
|
Hi James, it's posts like yours (thinking outside the box) that lets me learn something new every day... (+1 for that). Kind regards, Xander Well I appreciate that! I really like seeing the ways to write more "pythonic" --- it's so difficult coming from the C#/.NET/ArcObjects world to realign my thought process.
... View more
02-20-2014
03:08 AM
|
0
|
0
|
3063
|
|
POST
|
Just a shot in the dark here, but could the problem be the file extension? Are you sure it is ".xls" and not ".xlsx"?
... View more
02-20-2014
03:03 AM
|
0
|
0
|
1113
|
|
POST
|
While we're at it, using part of the idea James suggested (the numpy part) and mixing it with some list comprehensions I came up with this: import arcpy, numpy
intbl = r'C:\Project\_Forums\CommaDelimitField\test.gdb\intable'
outtbl = r'C:\Project\_Forums\CommaDelimitField\test.gdb\tst01'
flds = ('number_','Code','Year_')
# list comprehensions
lst_in = [row for row in arcpy.da.SearchCursor(intbl, flds)]
lst_out = [(num, row[1], row[2]) for row in lst_in for num in row[0].split(', ')]
# use numpy to store the table
npa = numpy.array(lst_out, numpy.dtype([('number', '|S10'), ('code', '|S10'), ('year', numpy.int32)]))
arcpy.da.NumPyArrayToTable(npa, outtbl, ("number", "code", "year"))
Kind regards, Xander Nice! I pulled from one of our impmentations with lots of pandas DataFrame processing, but I like this suggestion for just numpy work.
... View more
02-20-2014
02:56 AM
|
0
|
0
|
3063
|
|
POST
|
Just to be different and offer an alternative, you could employ the numpy and pandas libraries. I just tend to do lots of dev outside of the ESRI stack for processing things, so this might be handy.
import arcpy
import pandas as pd
import numpy as np
input_table = r'H:\Documents\ArcGIS\Default.gdb\fliptab'
##convert the input_table into a numpy array
nparr = arcpy.da.TableToNumPyArray(input_table, ['number', 'code', 'year'])
##convert the array into a pandas data frame
df = pd.DataFrame(nparr.tolist(), columns=['number', 'code', 'year'])
##deconstruct the column containing the commas and transpose into rows
s = df['number'].apply(lambda x: pd.Series(x.split(','))).stack()
s.index = s.index.droplevel(-1)
s.name = 'number'
##join the original dataframe and the temp series
del df['number']
outdf = df.join(s)
##convert the df into a numpy array
out_nparr = np.array(outdf.to_records(), np.dtype([('code', '|S10'), ('year', np.int32), ('number', '|S10')]))
outTable = r'H:\Documents\ArcGIS\Default.gdb\fliptab_output'
##finally covert the numpy array back into the gdb table
arcpy.da.NumPyArrayToTable(out_nparr, outTable, ("code", "year", "number"))
... View more
02-19-2014
09:47 AM
|
1
|
0
|
3063
|
|
POST
|
I think what Jake might be getting at is perhaps you are accessing the output FGDB workspace at the same time somewhere else in your code or not releasing a previous reference to it. I'd think that would cause accessibility issues on subsequent attempts to write to the workspace.
... View more
02-07-2014
09:40 AM
|
0
|
0
|
2430
|
|
POST
|
You just navigate to "Database Connections" in the ArcCatalog tree and go to Add Database Connection. Do you have an SDE instance installed on your SQL Server? http://resources.arcgis.com/en/help/main/10.1/index.html#/Enabling_SQL_Server_Express_to_store_geodatabases/018t0000000w000000/
... View more
01-30-2014
10:31 AM
|
0
|
0
|
1331
|
|
POST
|
I tried the following and got on line 21, Proposed Zoning layer data source in the 1st mxd did not update.
C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_1.mxd
<geoprocessing Map object object at 0x02965400>
Traceback (most recent call last):
File "C:\GIS\Python Scripts\Change Data Source MXD 2.py", line 21, in <module>
lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits")
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 680, in replaceDataSource
return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error
Code
import arcpy, os
from arcpy import env
from arcpy import mapping
arcpy.env.overwriteOutput = True
path = r'C:\GIS\MAPBOOK\Proposed Zoning Book'
for fileName in os.listdir(path):
fullPath = os.path.join(path, fileName)
if os.path.isfile(fullPath):
basename, extension = os.path.splitext(fullPath)
if extension == ".mxd":
mxd = arcpy.mapping.MapDocument(fullPath)
print fullPath
print mxd
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == "PROPOSED ZONING":
lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\DSD\DSD.DBO.MUNICIPALITY", "FILEGDB_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING")
elif lyr.name == "CITY LIMITS":
lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits")
print "Successfully updated data sources"
mxd.save
So what I have provided does actually get you past your initial error/problem and now you have come across a new one: your layer reference is invalid. ".DBO" sounds more like something from a SDE/Database schema, not a file geodatabase or something on disk in a folder. SQLEXPRESS.gds? I don't think I've seen that one before. You can access SDE data by pointing to the .sde file http://resources.arcgis.com/en/help/main/10.1/index.html#/Layer/00s300000008000000/
... View more
01-29-2014
09:55 AM
|
0
|
0
|
1753
|
|
POST
|
I started a new window and retyped the script and i got past the AttributeError: 'unicode' object has no attribute '_arc_object' error. but none of the workspace paths were updated. it error out on me on line 18. That is because you are passing "mxd" as a string. It needs to be a geoproc object. This will work:
path = r'C:\GIS\MAPBOOK\Proposed Zoning Book'
for fileName in os.listdir(path):
fullPath = os.path.join(path, fileName)
if os.path.isfile(fullPath):
basename, extension = os.path.splitext(fullPath)
if extension == ".mxd":
mxd = arcpy.mapping.MapDocument(fullPath)
print fullPath
print mxd
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == "PROPOSED ZONING":
lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\DSD\DSD.DBO.MUNICIPALITY", "SDE_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING")
elif lyr.name == "CITY LIMITS":
lyr.replaceDataSource(r"C:\Users\t**a\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits")
print "Successfully updated data sources"
mxd.save
... View more
01-29-2014
08:44 AM
|
0
|
0
|
1753
|
|
POST
|
This works to get you past the problem of referencing the layers in each of the .mxd's (I am just printing them lyr name, but you could continue to implement your desired process from that point on):
import os
import arcpy
path = r'C:\GIS\MAPBOOK\Proposed Zoning Book'
for fileName in os.listdir(path):
fullPath = os.path.join(path, fileName)
if os.path.isfile(fullPath):
basename, extension = os.path.splitext(fullPath)
if extension == ".mxd":
mxd = arcpy.mapping.MapDocument(fullPath)
for lyr in arcpy.mapping.ListLayers(mxd):
print lyr
edit: you need to add the import os "fullpath" references corrected to "fullPath" throughout. sorry (case is important!).
... View more
01-29-2014
08:14 AM
|
0
|
0
|
1753
|
|
POST
|
Why are you resetting mxd once you start to loop over the list of these mxd's?
mxdList = arcpy.ListFiles("*.mxd")
for mxd in mxdList: #<-- you already have the mxd set here
mxd = workspace + "//" + mxd #<-- now you are changing it right after you start the loop? why?
for lyr in arcpy.mapping.ListLayers(mxd[0]): #<--mxd is likely incorrect now
Instead, remove the mxd setting after the loop
mxdList = arcpy.ListFiles("*.mxd")
for mxd in mxdList: #<-- you already have the mxd set here, just keep it like that
for lyr in arcpy.mapping.ListLayers(mxd[0]): #<--mxd should now be fine
.
.
.
... View more
01-29-2014
06:41 AM
|
0
|
0
|
2123
|
|
POST
|
What you reference was just an example of accessing a FeatureClass directly, just as you would from a FGDB, i.e. C:\FGDB.gdb\FeatureClass. When you do the same thing in SDE the path includes the DB connection file + the DB + the schema + the featureclass name. But that is all really irrelevant. I'm just trying to find a way to have python on one cloud server access an SDE instance on another cloud server, presumably using a connection file I can create on the non-SDE server. I created a connection file like this and tested it. It worked in the sense that it did not raise an error, but it also did not give me the result I was expecting. Right, I assumed you are attempting to access the .ListFeatureClasses() with "DB connection file + the DB" and not "DB connection file + the DB + the schema + the featureclass name". That is, Do this: sdeFC = r"<remote db server>\SDE_Connection.sde"
for fc in arcpy.ListFeatureClasses():
print fc Not this: sdeFC = r"<remote db server>\SDE_Connection.sde\DB.dbo.FeatureClass"
for fc in arcpy.ListFeatureClasses():
print fc
... View more
01-21-2014
06:34 AM
|
0
|
0
|
2558
|
|
POST
|
In reading your post again, you mention that you can't programmatically Calculate Statistics; I'm able to do it with the following code: ##Calculate Statistics.
arcpy.AddMessage("Calculating Statistics for " + ras + ".")
arcpy.CalculateStatistics_management(ras, "1", "1", "#", "OVERWRITE")
We successfully run statistics with CalculateStatistics_management and we can verify that it completed by looking at the properties of the Mosaic. The problem is that if we load the Mosaic into ArcMap 10.1 and access the symbology tab, "Classified" is not available. BUT, if we right-click the Mosaic in catalog and run Enhancement-->Caclulate Statistics then load the Mosaic into ArcMap, "Classified" is available in the symbology tab. --Manually Calc statistics: Okay. --Programatically Calc Statistics: It does it ok, but we cannot access the "Classified" properties after it completes. I posted about this already and only solicited a reply that stated there are known issues and to submit a help ticket with ESRI.
... View more
01-21-2014
06:29 AM
|
0
|
0
|
1228
|
|
POST
|
The code was just something benign like this. I run it from IDLE and it completes without raising an error, but also without printing any featureclass names.
import arcpy
sdeConn = r"<connection file>" ##this is the one that was created with arcpy.management.CreateDatabaseConnection
arcpy.env.workspace = sdeConn
for fc in arcpy.ListFeatureClasses():
print fc
In the absence of try/except IDLE should raise any error. I am unfamiliar with attempting to access items from a particular schema like you are "DB.sde\DB.dbo.FeatureClass". Have you tried to ListFeatureClasses() with just the .sde connection alone? sdeFC = r"<remote db server>\SDE_Connection.sde"
... View more
01-21-2014
03:48 AM
|
0
|
0
|
2558
|
|
POST
|
To test it I set the connection file as a workspace and ran arcpy.ListFeatureClasses() against it. It did not raise any errors, but it also did not return any featureclasses that are in SDE. Can you post this code? Did you include error checking in a try/except block?
... View more
01-21-2014
03:20 AM
|
0
|
0
|
2558
|
|
POST
|
I have some Python scripting that creates two Raster Mosaic Datasets. The script then proceeds to add a number of surfaces simulating groundwater elevations. All of that is working well, but then I want to apply a color-ramped classified symbology from a layer file. My layer file contains a classified symbology. But my Raster Mosaic Datasets have a "Stretched" symbology. My understanding is that the symbologyType property is ReadOnly and cannot be changed. So my question is: Can the symbologyType of the Image layer in a Raster Mosaic Dataset be assigned to "Classified" when it's created? Thanks for any insight you can provide. Jon Mulder Try to calculate statistics on the Mosaic. We are doing the same and manually right-click the mosaic in catalog tree then thru the Enhancements menu, choose Calculate Statisics (using all the defaults). Now when you access the Mosaic's properties, Classified should be available (which you can then apply the renderer of your choice). However, when we attempt to programmatically Calculate Statisicts, arcpy.CalculateStatistics_management, it doesn't appear to work. So, I hope someone can chime in with some additional insight.
... View more
01-21-2014
02:40 AM
|
0
|
0
|
1228
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 02-17-2020 10:47 AM | |
| 1 | 10-25-2022 11:46 AM | |
| 1 | 08-08-2022 01:40 PM | |
| 1 | 02-15-2019 08:21 AM | |
| 2 | 08-14-2023 07:14 AM |
| Online Status |
Offline
|
| Date Last Visited |
01-22-2025
02:28 PM
|