Command for exporting rasters to points

838
5
Jump to solution
02-03-2014 11:19 PM
SonjaVoncina
New Contributor
Hi!
Can anyone help me with Python code for exporting many rasters to points.
I have numerous raster files I want to convert to points. I want program to open every raster in a folder and save its point file to another folder, name must stay the same.

I have diffilculties defining inRaster and outRaster as I'm a newbie in Python language.

Thank you.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
There is one minor mistake in the code that Anthony posted.  The code is trying to convert the entire rasterlist to a point feature class rather than the individual raster.  Try the following:

import arcpy from arcpy import env  arcpy.env.workspace = r"C:\GIS\Projekti\Divaca_Ber_10ocen_met9\Testno\script" outworkspace = r"C:\GIS\Projekti\Divaca_Ber_10ocen_met9\Testno\script\point"  rasterlist = arcpy.ListRasters("*")  for raster in rasterlist:   arcpy.RasterToPoint_conversion(raster, outworkspace + "//" + str(raster) + ".shp", "VALUE")  

View solution in original post

0 Kudos
5 Replies
AnthonyCheesman1
Occasional Contributor II
Hi

The 'raster to point' tool will be able to help.

A couple of questions:

1/ are all the rasters in the same directory?
2/ where do you want to store the output files?
3/ how do you want to name the output files?

It would be quite straightforward if all the rasters are in the same folder, and you want to store all of the point feature classes in the same location (ie in a folder if working with shapefiles, or in a geodatabase if working with feature clases)

The easiest thing to do would be to write a simple loop, using a list to contain all the names of the raster files.

I don't have a arcpy-enabled machine with me tonight, so the code below isn't absolutely correct or validated, however you will want to do something like this:

#import libraries

import arcpy
from arcpy import env

#set environments

arcpy.env.workspace = r'[file path to where your rasters are stored]
outworkspace = r'[file path to where you want to save outputs]

#create list

rasterlist = arcpy.ListRasters("*")

#loop thru list

for raster in rasterlist:
  arcpy.RasterToPoint_conversion(rasterlist, outworkspace + "//" + str(raster) + "_point", "VALUE")
  #this confirms you are looking at the VALUE field of the raster, and calling the output file the same name as the raster, appended by _point.

del outworkspace, raster, rasterlist



Good luck.
0 Kudos
SonjaVoncina
New Contributor
1/ are all the rasters in the same directory? YES
2/ where do you want to store the output files? In another directory.
3/ how do you want to name the output files? Same as raster file.


>>> #import libraries
... import arcpy
... from arcpy import env
... #set environments
... arcpy.env.workspace = r"C:\GIS\Projekti\Divaca_Ber_10ocen_met9\Testno\script"
... outworkspace = r"C:\GIS\Projekti\Divaca_Ber_10ocen_met9\Testno\script\point"
... #create list
... rasterlist = arcpy.ListRasters("*")
... #loop thru list
... for raster in rasterlist:
... arcpy.RasterToPoint_conversion(rasterlist, outworkspace + "//" + str(raster) + "_point", "VALUE")
... #this confirms you are looking at the VALUE field of the raster, and calling the output file the same name as the raster, appended by _point.
... del outworkspace, raster, rasterlist
...
Runtime error <type 'exceptions.RuntimeError'>: Object: Error in executing tool
>>>


This is the result. Is my path to folder correct (marked in red)?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
There is one minor mistake in the code that Anthony posted.  The code is trying to convert the entire rasterlist to a point feature class rather than the individual raster.  Try the following:

import arcpy from arcpy import env  arcpy.env.workspace = r"C:\GIS\Projekti\Divaca_Ber_10ocen_met9\Testno\script" outworkspace = r"C:\GIS\Projekti\Divaca_Ber_10ocen_met9\Testno\script\point"  rasterlist = arcpy.ListRasters("*")  for raster in rasterlist:   arcpy.RasterToPoint_conversion(raster, outworkspace + "//" + str(raster) + ".shp", "VALUE")  
0 Kudos
SonjaVoncina
New Contributor
IT WORKS, IT WORKS!!
Thank you very much. 🙂
0 Kudos
AnthonyCheesman1
Occasional Contributor II
Thanks Jake for picking up the error - I did notice that as I re-read the post over breakfast this morning.

Glad the process worked.
0 Kudos