#--------------------- Program LatLongGrid Documentation ------------------- # LatLongGrid.py: program to construct a grid of latitude-longitude points in # a text file. The text file can be imported into ArcGIS 10.1 as decimal # lat-long points (geographic) that can be used to georeference scanned # raster images that contain lat-long reference points. Input for the program # is taken from the command line: # 1st parameter: output file name # 2nd parameter: lower left (SW) corner longitude {+/-}DDDMMSS format # 3rd parameter: lower left (SW) corner latitude {+/-}DDMMSS format # 4th parameter: upper right (NE) corner longitude {+/-}DDDMMSS format # 5th parameter: upper right (NE) corner latitude {+/-}DDMMSS format # 6th parameter: longitude spacing for grid in DDDMMSS format # 7th parameter: latitude spacing for grid in DDMMSS format # The output text file will be a comma delimited 3 column format: # 1st column: longitude coordinate in decimal degree format. # 2nd column: latitude coordinate in decimal degree format. # 3rd column: grid point number starting with 1 at SW corner and incremented # by row and then column. Last (highest) point number is the NE corner. # #-------------------------------------------------------------------------- import arcpy import sys #----------------------- Function Definitions ----------------------------- def LongStr2DD(longstr): long_dd = 0.0 sfact = 1.0 if longstr[0] == "-" : sfact = -1.0 long_dd = float(longstr[1:4])+float(longstr[4:6])/60.0+float(longstr[6:8])/3600.0 long_dd = long_dd * sfact return long_dd def LatStr2DD(latstr): # Insert your code here! return lat_dd def CheckLongStr (longstr): if len(longstr) == 7: longstr = "+"+longstr return longstr def CheckLatStr(latstr): # Insert your code here! return latstr #------------------------ Main Program Code ------------------------------- # Initialize constants longstrlen = 8 latstrlen = 7 ofn="grid.txt" dl = "," eoln = "\n" err_msg = "unknown error" # get input from parameter string. Test program with below examples: # ex. c:/temp/grid.txt -0860000 +325230 -0855230 +330000 000230 000230 (Alex City AL 1:24,000) # ex. c:/temp/grid.txt -0870000 +320000 -0860000 +323000 000730 000730 (Montgomery AL 1:100,000) # ex. c:/temp/grid.txt -0880000 +320000 -0860000 +330000 001500 001500 (Montgomery AL 1:250,000) try: ofn = sys.argv[1] SW_longstr = CheckLongStr(sys.argv[2]) SW_latstr = CheckLatStr(sys.argv[3]) NE_longstr = CheckLongStr(sys.argv[4]) NE_latstr = CheckLatStr(sys.argv[5]) long_spcstr = sys.argv[6] lat_spcstr = sys.argv[7] # check parameter strings for proper length if len(SW_longstr) <> longstrlen : err_msg = "ERROR: SW corner longitude parameter incorrect format" raise exception if len(SW_latstr) <> latstrlen : err_msg = "ERROR: SW corner latitude parameter incorrect format" raise exception if len(NE_longstr) <> longstrlen : err_msg = "ERROR: NE corner longitude parameter incorrect format" raise exception if len(NE_latstr) <> latstrlen : err_msg = "ERROR: NE corner latitude parameter incorrect format" raise exception if len(long_spcstr) <> 6 : err_msg = "ERROR: longitude spacing parameter incorrect format" raise exception if len(lat_spcstr) <> 6 : err_msg = "ERROR: latitude spacing parameter incorrect format" raise exception # open text file for write output of = open(ofn,"w") of.write("Longitude"+dl+"Latitude"+dl+"No."+eoln) sw_long_dd = LongStr2DD(SW_longstr) sw_lat_dd = LatStr2DD(SW_latstr) ne_long_dd = LongStr2DD(NE_longstr) ne_lat_dd = LatStr2DD(NE_latstr) longspc_dd = LongStr2DD("+0"+long_spcstr) latspc_dd = LatStr2DD("+"+lat_spcstr) nrows = # insert your expression here! ncols = # insert your expression here! count = 0 i = 0 while i < nrows: lat_dd = sw_lat_dd + i * latspc_dd i = i + 1 j = 0 while j < ncols: long_dd = sw_long_dd + j * longspc_dd j = j + 1 count = count + 1 of.write(str(long_dd)+dl+str(lat_dd)+dl+str(count)+eoln) print ofn+" has been sucessfully created."+eoln except (IndexError): err_msg = "No command line parameters or missing command line parameter!" print err_msg print "command line syntax: {output file} {SW long} {SW lat} {NE long} {NE lat} {long spacing} {lat spacing}" print "example command line: c:/temp/grid.txt -0860000 +325230 -0855230 +330000 000230 000230" except: print err_msg print "example command line: c:/temp/grid.txt -0860000 +325230 -0855230 +330000 000230 000230" finally: # close the output file if not(of.closed) : of.close() #----------- Program End ------------------------------------------------