PolygonToRaster in stand alone script

679
5
07-07-2017 02:25 PM
LauraConner
New Contributor

my code is:

print "running..."
import arcpy
import arcpy, os
from arcpy import env
import os
import os.path
env.workspace =r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\route_polygons.mxd"


global shp
shp = ".shp"

global tif
shp = ".tif"


x=1

in_features= r'P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\meters.shp'
out_layer = 'meters_temp'
arcpy.MakeFeatureLayer_management (in_features, out_layer,)

in_features2= r'P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\parcels.shp'
parcles_temp = 'parcles_temp'
arcpy.MakeFeatureLayer_management (in_features2, parcles_temp,)

while x <= 2:
    import arcpy
    import arcpy, os
    from arcpy import env
    import os
    import os.path

    # slection fuction: select meters with x for cycle/ route
    layer = out_layer
    criteria = 'route_sequ =' + str(x)
    arcpy.SelectLayerByAttribute_management (layer,'NEW_SELECTION',criteria)
    
    #slect by location: select parcles by selected meters
    in_layer = parcles_temp
    selector = out_layer
    distance = 60
    arcpy.SelectLayerByLocation_management (in_layer,select_features = selector, search_distance=distance )

    #copy features to shape file
    in_features = parcles_temp
    out_feature_class = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x) + shp
    arcpy.CopyFeatures_management(in_features, out_feature_class)

    #merge polpgons in to 1
    in_features = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x) + shp
    out_feature_class = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x)+'merge' + shp
    arcpy.Dissolve_management (in_features, out_feature_class, multi_part='MULTI_PART')

    # add feild
    in_table = out_feature_class
    field_name = "Route"
    field_type = 'TEXT'
    arcpy.AddField_management (in_table, field_name, field_type, field_length=10)

    #populate feild
    in_table =out_feature_class
    field= "Route"
    expression = x
    arcpy.CalculateField_management (in_table, field, expression)

    #add cycle feild
    in_table = out_feature_class
    field_name = "Cycle"
    field_type = 'TEXT'
    arcpy.AddField_management (in_table, field_name, field_type, field_length=10)

    #apend in to database
    inputs = out_feature_class
    target = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\route_polygons.shp"
    arcpy.Append_management (inputs, target, schema_type='NO_TEST')

    # expoert to raster
    in_features =r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x)+'merge' + shp
    value_field = "Route"
    out_rasterdataset = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\TIFF" + str(x) + ".tif"
    arcpy.PolygonToRaster_conversion (in_features, value_field, out_rasterdataset)

    print x
    x=x+1
print "done"

everything works except for the last function PolygonToRaster. the error i get is:

Traceback (most recent call last):
  File "P:\GIS Public\Projects\Meter Zones\route_polygons\make_route_pologons.py", line 53, in <module>
    arcpy.Dissolve_management (in_features, out_feature_class, multi_part='MULTI_PART')
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 4192, in Dissolve
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\1.tif does not exist or is not supported
Failed to execute (Dissolve).

i need to export the files in to a TIFF file. i know  something wrong with the line out_rasterdataset = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\TIFF" + str(x) + ".tif". can you help with the syntax?

thank you

0 Kudos
5 Replies
RandyBurton
MVP Regular Contributor

Several lines in your script use r"P:\\GIS Public\\Projects\\...  You do not need to escape your slashes by doubling them; the "r" indicates it is a raw string.

# Instead of:
out_rasterdataset = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\TIFF" + str(x) + ".tif"

# Use:
out_rasterdataset = r"P:\GIS Public\Projects\Meter Zones\route_polygons\TIFF" + str(x) + ".tif"

In the discussion of String literals, see the paragraph about using the 'r' prefix.

SteveLynch
Esri Regular Contributor

...the text in red points to an issue in line 53. It also informs you that there is an issue with Dissolve.

MicahBabinski
Regular Contributor

Hi Laura,

From what I can see it looks like your last declaration of the shp variable says:

shp = ".tif"

I think you meant to have it say:

shp = ".shp"

Since .tif is a raster format, and the in_features parameter of the Dissolve tool needs to be a feature layer/feature class, that is why it won't work. The output feature class for a Dissolve should be a vector dataset like a Shapefile as well.

Also, when needing to combine strings to make paths in Python, I really like to use os.path.join(path, {filename.extension}) so I don't have to use a bunch of + operators in my string. It might be worth considering to minimize confusion.

10.1. os.path — Common pathname manipulations — Python 2.7.13 documentation 

Hope this helps!

Micah

DanPatterson_Retired
MVP Esteemed Contributor

Concur with all of the above...

It would be easier to read your code if you could format your code next time as well

Code Formatting the Basics++

curtvprice
MVP Esteemed Contributor

I have some more advice that may help you out. Good luck!

import arcpy, os

### standard practice is to put each import on its own line
### import os
### import arcpy

from arcpy import env

import os
import os.path
### the above two lines can be deleted

env.workspace =r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\route_polygons.mxd"
### env.workspace should be set to a workspace not an mxd, for example:
### env.workspace = r"P:\GIS Public\Projects\Meter Zones\route_polygons"


global shp
shp = ".shp"

global tif
shp = ".tif"

### no need for global variables. They are generally to be avoided anyway
### all variables set at this top level (ie not in a function) are
### global to this python script without declaring global

x=1

in_features= r'P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\meters.shp'

### if you set the env.workspace to the folder above you can avoid the path:
### in_features = "meters.shp"

out_layer = 'meters_temp'
arcpy.MakeFeatureLayer_management (in_features, out_layer,)

in_features2= r'P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\parcels.shp'
parcles_temp = 'parcles_temp'
arcpy.MakeFeatureLayer_management (in_features2, parcles_temp,)

while x <= 2:
    import arcpy
    import arcpy, os
    from arcpy import env
    import os
    import os.path

    ### not necessary to re-import, the imports are good for the whole script

    # slection fuction: select meters with x for cycle/ route
    layer = out_layer
    criteria = 'route_sequ =' + str(x)

    ### formatting functions make this much easier:
    ### criteria = "ROUTE_SEQU = {}".format(x)

    arcpy.SelectLayerByAttribute_management (layer,'NEW_SELECTION',criteria)
    
    #slect by location: select parcles by selected meters
    in_layer = parcles_temp
    selector = out_layer
    distance = 60
    arcpy.SelectLayerByLocation_management (in_layer,select_features = selector, search_distance=distance )

    #copy features to shape file
    in_features = parcles_temp
    out_feature_class = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x) + shp
    arcpy.CopyFeatures_management(in_features, out_feature_class)

    #merge polpgons in to 1
    in_features = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x) + shp
    out_feature_class = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x)+'merge' + shp
    arcpy.Dissolve_management (in_features, out_feature_class, multi_part='MULTI_PART')

    ### again all the paths above could be shortened if env.workspace is set
    ### and string formatting used too. NOTE never start a dataset or field name
    ### with a number: you have been warned! I inserted the letter "t" here.
    ### in_features = "t{}.shp".format(x)

    # add feild
    in_table = out_feature_class
    field_name = "Route"
    field_type = 'TEXT'

    ### note ' and " can be used interchangeably, I usually use " it's a style thing.
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍