Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have over 100 shape files that don't have .prj file and thus when I bring them into ArcMap they show the coordinate system as unknown. I know all of the shape files coordinate system is GCS WGS 1984. I also know I can use the Define Projection GP tool to individually assign the coordinate system to each file but that will take forever.

I was hoping there was a GP tool to batch define these but I don't see one. Next I was thinking maybe I could use python to do this so I looked in the help menu and found a script but it gives me an error.

Here is the python code I tried (this is for a single shp file so I would still have the pain of typing the name for each file:

import arcpy
infc = r"C:\Documents and Settings\User\My Documents\ArcGIS\shpfiles\Site_2.shp"
prjfile = r"<install directory>\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
arcpy.DefineProjection_management(infc, prjfile)

Any help on this problem would be greatly appreciated.

share|improve this question
    
But If I want to define projection for multi raster files, what can I do? –  user22830 Oct 11 '13 at 4:24

6 Answers 6

up vote 18 down vote accepted

I think you guys are overthinking this one... Right-click on the "Define Projection" tool in toolbox, select "Batch", drag-and-drop your layers into the "Input Dataset" column, right-click in the first "Coordinate System" box to fill out the correct projection, then right-click on the projection you just selected and choose "Fill" which will fill out all of the rest of the projections for you. Hit "OK" and you are done.

alt text

share|improve this answer
    
Does that actually generate the Shapefile .PRJ helper file in the OS folder, or just annotate a layer attribute? And you'd have to launch ArcGIS as well. The Python script Jay put up will do its work completely outside ArcGIS, with certain results--good for non-ESRI needs. –  V Stuart Foote Jan 7 '11 at 23:26
    
Yes, the "Define Projection" tool writes a PRJ file for all shapefiles that are processed through it. While I agree the python script Jay posted will probably work great, scripting is often outside the skill set of an average GIS specialist, whereas the push-button "Define Projection" utility is not. –  RyanDalton Jan 7 '11 at 23:52
    
Thanks Ryan, I hand no idea that you could right-click on GP tool to get more options. That is a nice trick and was just what I was looking for. Much appreciated. –  wilbev Jan 8 '11 at 1:47

If they are in the same directory, something like this would work (just replace your paths, maybe add in some exception handling):

import os, shutil

wgs84prjpath='c:/Program Files/ArcGIS/Desktop10.0/Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj'

def definewgs84(shpfilepath):
    tgtpath = os.path.splitext(shpfilepath)[0] + '.prj'
    shutil.copyfile(wgs84prjpath,tgtpath)


yourshapefiledirectory='C:/temp/'
extension='shp'

shpfileslist = [file for file in os.listdir(yourshapefiledirectory) if file.lower().endswith(extension)]

for filename in shpfileslist:
    definewgs84(os.path.join(yourshapefiledirectory,filename))
share|improve this answer
    
I tried this route because I want to learn to use to Python more but I was not successful. It is erroring out on the 'os' name. I pasted in the error message below. Any ideas what may be wrong? –  wilbev Jan 8 '11 at 1:49
    
Sorry that got added before I pasted in the error, here it is: Runtime error <type 'exceptions.NameError'>: name 'os' is not defined –  wilbev Jan 8 '11 at 1:50
    
oops...the imports would help!: import os, shutil (see the updated the answer). docs.python.org/tutorial/stdlib.html should you there. –  Jay Cummins Jan 8 '11 at 1:52
    
Thanks Jay, that import code make it work, thanks. –  wilbev Jan 13 '11 at 22:36

Just make copies of the .prj and rename. So, e.g., if you have 3 shapefiles:

  • one.shp,
  • two.shp,
  • three.shp.

Define the projection for one.shp and you will have one.prj in the directory. Copy one.prj to the directory of two.shp and rename to two.prj, repeat for all shapefiles. The .prj is just a text file. As long as there is a .prj in the same directory as a .shp and with the same name, the software will pick it up. Automate with whatever tools you are familiar with for copying and renaming files.

share|improve this answer
    
+1 One of the simplest and fastest solutions. –  whuber Apr 20 '11 at 18:58

how about a simple shell script and ogr2ogr? See http://www.gdal.org/ogr2ogr.html for the exact syntax.

share|improve this answer

Edit:

Probably the easiest method would be. Bring them all in and assign the correct crs to the document. They would be "unprojected" but sitting in the right place. Then just export (multiple) to a new location.

I do see a GP tool for batch projection.

batch proj

It may not be exposed in arcpy. ??

I think I would create a pgdb, (File, personal or even sde) and then create a fds (feature data set).

Assign the appropriate crs to that fds.

Import the shape files (multiple). [all fc in a fds inherit the crs of the fds].

Then you can export to shape file (multiple).

This would be in lieu of batch projection, and I'm sure could be scripted.

share|improve this answer
    
Although this method does work, it takes much longer than what Ryan Dalton suggested. The import of over 100 shape files into a feature class is very slow. –  wilbev Jan 8 '11 at 1:51

Here's what I use...it will only define the projection for raster files which don't have a projection. Hope it helps. It also creates a list of the files without projection for Quality Assurance purposes.

For your vectors it just needs a slight mod - FileList=arcpy.ListFeatureClasses()

# Defines projection for all rasters with undefined projection
# CAUTION - make sure you know that the projection you are defining is the correct one
# for all files in the current directory.

#Licence: Creative Commons
#Created by: George Corea; georgec@atgis.com.au, coreagc@gmail.com

import arcpy, glob, os, sys, arcgisscripting
from arcpy import env, mapping
path = os.getcwd()
env.workspace = path
env.overwriteOutput = True

print 'Reading files from ' + path
os.chdir(path)
prjFile=r'C:\\Python26\\GDA_1994_MGA_Zone_55.prj'

x=0
z=x+1
NoProjCount=0
FileList= arcpy.ListRasters()
for File in FileList:
    desc = arcpy.Describe(File)
    SR = desc.spatialReference
    if SR.name == "Unknown":
        print "Projection of " + str(File) + " is " + SR.name + " so defining projection."
        f = open('NoProjection.txt', 'a')
        f.write(str(File)+"\n")
        f.close()
        arcpy.DefineProjection_management(File, prjFile) 
        NoProjCount=NoProjCount+1
    else:    
        print File + " is projected " + str(SR.name)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.