📄 batchdefineprojection.py
字号:
"""++++++++++++++++++++++++++++++++++++++++++++++++++++++
BatchDefineProjection.py
USAGE: <input_workspace> <projection> {data_type} {wildcard} {type} {overwrite_projection}
DESCRIPTION: This script is used to define a projection to a set of input feature classes or
rasters in a workspace.
The <input_workspace> can be a folder containing shapefiles or rasters or a geodatabase
containing feature classes or rasters. The geodatabase can be a personal or a enterprise database.
The <projection> can be set using a template dataset containing the projection to use
or a projection file (.prj). If the path contains spaces it must be quoted.
The {data_type} can be used to either define a projection to a list of feature classes or rasters.
Valid data types are:
* FEATURES - This is the default. Only get a list of feature classes in <input_workspace>.
* RASTERS - Gets a list of rasters in the <input_workspace>.
The {wildcard} can be used to filter the list of feature classes or rasters.
Combination of * and characters that will help limit the results.
The asterisk (*) is the same as saying ALL. If no wildcard is specified then all
feature classes or rasters, in the workspace, will be returned.
The {type} can be used to select feature classes of a
specific feature type or rasters of a specific type.
Valid FeatureTypes and RasterTypes for this method are:
* POINT - Only point feature classes will be returned.
* LABEL - Only label feature classes will be returned.
* NODE - Only node feature classes will be returned.
* LINE - Only line feature classes will be returned.
* ARC - Only arc feature classes will be returned.
* ROUTE - Only route feature classes will be returned.
* POLYGON - Only polygon feature classes will be returned.
* REGION - Only region feature classes will be returned.
** ALL - All feature classes will be returned. This is the default.
RASTER TYPES:
* ADRG, BIL, BIP, BSQ, BMP, CADRG, CIB, ERS,
GIF, GIS, GRID, STACK, IMG, JPEG, LAN, SID,
TIFF, RAW, PNG, NITF, ALL
The {overwrite_projection} can be used to overwrite the projection of any feature class
or raster that already has a projection defined. The default is set to do not overwrite.
Valid options are:
* NO - Do not overwrite the projection definition. This is the default.
* YES - Overwrite the projection definition if already defined.
AUTHOR: ESRI Inc., Redlands
DATE: August 18, 2004
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"""
# Create Geoprocessing Object
import win32com.client, sys
gp = win32com.client.Dispatch("esriGeoprocessing.GPDispatch.1")
# Usage string
usage = "<input_workspace> <projection> {data_type} {wildcard} {type} {overwrite_projection}"
# Set boolean for overwriting projection definition. Default is NO (false).
bDefineProject = 0
try:
#Check if all required parameters have been provided.
if len(sys.argv) < 3:
raise Exception, "Invalid number of parameters provided. \n" + "Usage: " + usage
# Set the input workspace environment
# Example: "C:\\Workspace" or "C:\\Workspace\\MyGDB.mdb"
gp.workspace = sys.argv[1]
#Check if <input_workspace> exists
if not gp.exists(gp.workspace):
raise Exception, "<input_workspace> does not exist."
# Set the projection information. Either a template dataset or projection file
# Example: "C:\\Workspace\\Roads.shp" or "C:\\ArcGIS\\Coordinate Systems\\Geographic Coordinate Systems\\World\\WGS 1984.prj"
prj = sys.argv[2]
# Check if template dataset or projection file exists.
if not gp.exists(prj):
raise Exception, "Template dataset or projection file does not exist. " + prj
if len(sys.argv) < 4:
data_type = "FEATURES"
else:
data_type = sys.argv[3]
if data_type == "#":
data_type = "FEATURES"
# Set the wildcard option. The Default is "*".
if len(sys.argv) < 5:
wildcard = "*"
else:
wildcard = sys.argv[4]
if wildcard == "#":
wildcard = "*"
# Set optional argument for type. Default is "ALL".
if len(sys.argv) < 6:
type = "ALL"
else:
type = sys.argv[5]
if type == "#":
type = "ALL"
# Set optional argument for overwrite projection definition. Default is "NO"
if len(sys.argv) < 7:
overwrite_projection = "NO"
else:
overwrite_projection = sys.argv[6]
if overwrite_projection == "#":
overwrite_projection = "NO"
#Loop through each featureclass or raster in input workspace and define a projection
if data_type == "FEATURES":
datasets = gp.listfeatureclasses(wildcard, type)
dataset = datasets.Next()
else:
#data_type is RASTERS
datasets = gp.listrasters(wildcard, type)
dataset = datasets.Next()
while dataset:
# If {overwrite_projection} is NO,
# check if the feature class has a projection already define by doing a describe.
if overwrite_projection == "NO":
dsc = gp.Describe(dataset)
if dsc.SpatialReference.Name == "Unknown":
bDefineProject = 1
else:
print "Projection already defined and will not be changed."
else:
# {overwrite_projection} is "YES"
bDefineProject = 1
if bDefineProject == 1:
try:
gp.defineprojection_management(dataset, prj)
print "Successfully defined projection for " + dataset
except:
# Print Error Messages
print gp.getmessages(2)
dataset = datasets.Next()
except Exception, ErrDesc:
print ErrDesc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -