Select to view content in your preferred language

ArcPy and Rasters

73
3
Monday
Labels (3)
GIS_Rabbit
Emerging Contributor

Hello! I'm currently working on a script that seeks to pull data from an image server and then preprocess this data by clipping it to a study area extent using the raster functions data management clip tool, feed that clipped data into a GeoAI algorithm, and then save then saves the output after the fact.

The issue I am having is that I cannot seem to convert the data that I added from path to a Raster object. It gets added to my map just fine, and I have been able to print the name of the data, just to be sure it actually gets added. 

The reason for wanting to use the raster function's clip and other data management tools is because it is faster and doesn't save anything to file unless explicitly told to. I have run the process in pro and it seems to work well, but we're looking to automate the process and avoid using the GUI for this.

Here's a snippet of my code so far. Thank you all for your expert opinions!

Code:

import arcpy
arcpy.CheckOutExtension("Spatial")
from arcpy.sa import *
from arcpy.ia import *
import geopandas as gpd
 
env = input("Enter the workspace environment: ")
if env.startswith('"') or "\\" in env:
    env = env.replace('"', '').replace('\\', '/')
try:
    arcpy.env.workspace = env
    arcpy.env.overwriteOutput = True

    serverDataPath = input("Enter the server data path: ")
    serverDataPath = serverDataPath.replace('\\', '/')
    serverDataPath = r"{}".format(serverDataPath)

    #Load Up the Pro Project
    aprx = input("Enter the path to the ArcGIS Pro project (.aprx): ")
    if aprx.startswith('"') or "\\" in aprx:
        aprx = aprx.replace('"', '').replace('\\', '/')
    aprx = arcpy.mp.ArcGISProject(aprx)

    #Create a disposable Map
    mapCheck = aprx.listMaps()
    for m in mapCheck:
        if m.name == "serverData":
            aprx.deleteItem(m)
    else:
        map = aprx.createMap("serverData", "Map")
        print("{} map created successfully".format(map.name))

    #Add the server data path to the map
    map.addDataFromPath(serverDataPath)
except Exception as e:
    print("Error: {}".format(e))
 
#List Items
raster = None
try:
    items = map.listLayers()
    for i in items:
        if i.name != "Topographic":
            raster = i
except Exception as e:
    print("Error listing raster items: ", e)
 
#Convert to Raster object
rasterFile = Raster(raster)

Error:
TypeError Traceback (most recent call last) Cell In[24], line 1----> 1rasterFile = Raster(raster) File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Raster.py:82, in Raster.__new__(cls, in_raster, is_multidimensional) 81 def __new__(cls, in_raster, is_multidimensional=False ---> 82 return super().__new__(cls, in_raster, is_multidimensional) TypeError: expected a raster or layer name
Tags (3)
0 Kudos
3 Replies
Clubdebambos
MVP Regular Contributor

Hi @GIS_Rabbit,

Can you please use the code snippet format to make it easier to read?

Select the ellipsis (...)

Clubdebambos_0-1754983619722.png

Select Insert/Edit code sample...

Clubdebambos_1-1754983667519.png

Select Python from the language dropdown

Clubdebambos_2-1754983713597.png

All the best,

Glen

 

 

~ learn.finaldraftmapping.com
0 Kudos
GIS_Rabbit
Emerging Contributor

Thank you for that @Clubdebambos! First time making a post here so I wasn't aware of this. 

0 Kudos
GIS_Rabbit
Emerging Contributor

For whatever reason, the site doesn't like it when i do this. Keeps showing this, even though there are no invalid html. When I try to accept the changes it claims it made, it brings me back. 

GIS_Rabbit_0-1755092185653.png

 

0 Kudos