'''---------------------------------------------------------------------------------- Tool Name: MRO_Export_MDB Source Name: MRO_Export_MDB.py Version: ArcGIS 9.2 date: Apr 2008 Author: Trent Hare This script will create a MRO ASCII file from an Access Personal GDB for importing into the target MRO HiCAT database ----------------------------------------------------------------------------------''' #test arguments #E:\\GIS\\Mars\\MRO\\roi_arcmap\\HiCAT_geographic.mdb\\GCS_MARS_2000\\dd_temp e:\\temp\\out.txt "Ographic to Ocentric" def go_tlac(show_msg): # - printing message gp.addmessage(show_msg); print show_msg def go_error(show_error): # - error gp.addmessage(show_error); print show_error sys.exit() def isnumeric(myvar): #checks via type() if myvar is numeric or not''' if type(myvar)==type(1) or type(myvar)==type(1L) or \ type(myvar)==type(1.1) or type(myvar)==type(1+1j) : return 1 else: return 0 def oc2og(dlat, dMajorRadius, dMinorRadius): try: dlat = 3.14159265358979 / 180 * dlat #math.radians(dlat) dlat = math.atan(((dMajorRadius / dMinorRadius)**2) * (math.tan(dlat))) dlat = 180 / 3.14159265358979 * dlat #math.degrees(dlat) except: sErrors = gp.GetMessages(2); go_tlac(sErrors) return dlat def og2oc(dlat, dMajorRadius, dMinorRadius): try: dlat = 3.14159265358979 / 180 * dlat #math.radians(dlat) dlat = math.atan((math.tan(dlat) / ((dMajorRadius / dMinorRadius)**2))) dlat = 180 / 3.14159265358979 * dlat #math.degrees(dlat) except: sErrors = gp.GetMessages(2); go_tlac(sErrors) return dlat #-------------------------------------------------------------------------- #Imports #-------------------------------------------------------------------------- import string, math, os, sys, locale try: import arcgisscripting gp = arcgisscripting.create() gp.AddMessage("\n" + "Using ArcMap 9.2 or above with arcgisscripting..." + "\n") except: import win32com.client gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") gp.AddMessage("\n" + "Using ArcMap 9.0/9.1 or above with win32com.client.Dispatch..." + "\n") gp.overwriteoutput = 1 msgNotEnoughParams = "Incorrect number of input parameters." try: if len(sys.argv) < 3: raise Exception, msgNotEnoughParams inputFC = sys.argv[1] outName = sys.argv[2] latConvert = sys.argv[3] #create output outFile = open(outName, "w") #grab path thePath = os.path.split(outName)[0] if latConvert == "Ographic to Ocentric": og2oc_convert = 1 elif latConvert == "Ocentric to Ographic": oc2og_convert = 1 else: og2oc_convert = 0 oc2og_convert = 0 inDesc = gp.describe(inputFC) #Get feature layer's projection srs_Dataset = inDesc.SpatialReference #go_tlac("Input Projection: "+ str(srs_Dataset)) #Define WKT Mars 2000 projection crs_Mars2000 = "GEOGCS['GCS_Mars_2000',DATUM['D_Mars_2000',SPHEROID['Mars_2000_IAU_IAG',3396190.0,169.8944472236118]],PRIMEM['Reference_Meridian',0.0],UNIT['Degree',0.0174532925199433]]" outCRS = gp.CreateSpatialReference(crs_Mars2000) go_tlac("output Projection: " + outCRS + "\n\n") try: outfc = thePath + "\\xxtmpout2.shp" if gp.Exists(outfc): gp.Delete(outfc) gp.CreateFeatureclass_management(thePath,"xxtmpout2.shp","Point","","Disabled","Disabled",outCRS) srOut = gp.describe(outfc).SpatialReference if gp.Exists(outfc): gp.Delete(outfc) except: print "Error creating xxtmpout2.shp with",outCRS sys.exit(1) theFields = (["PRIORITY_OVERALL","SCIENCE_RATIONALE","DESCRIPTION","PRIMARY_SCIENCE_THEME",\ "SECONDARY_SCIENCE_THEME","PRIORITY_STEREO","PRIORITY_COLOR","CRISM_COORDINATION",\ "JUSTIFICATION_COLOR_STEREO_CRISM","PRIORITY_HIGHRES","MAX_BINNING",\ "JUSTIFICATION_HIGHRES","MAX_LSUBS","MIN_LSUBS","NUMBER_OF_OBSERVATIONS",\ "MAX_PHASE_ANGLE","MIN_PHASE_ANGLE","MAX_INCIDENCE_ANGLE","MIN_INCIDENCE_ANGLE",\ "MAX_EMISSION_ANGLE","MIN_EMISSION_ANGLE","SPECIAL_NOTES","USERNAME"]) #go_tlac("fields: " + str(theFields.count)) #Example: rows = GP.SearchCursor("D:/St_Johns/data.mdb/buildings", "",desc.SpatialReference) #Old method: inRows = gp.searchcursor(theTempFile, "",outCRS) inRows = gp.searchcursor(inputFC, "", srOut) inRow = inRows.next() while inRow: sOut = "" iFld = 0 feat = inRow.GetValue(inDesc.ShapeFieldName) #Reproject feature to decimal degree for aField in theFields: iFld = iFld + 1 #Grab value from the field try: sValue = inRow.GetValue(aField) except: show_err = "It appears the Field: " + aField + "does not exist! Exiting."; go_error(show_err) #Check if Field is a String, if so add quotes if not isnumeric(sValue): #show_msg = "Value: " + sValue ; go_tlac(show_msg) if sValue == None: sValue = '""' else: #it contains a string in the field, replace special chars and add quotes sValue = string.replace(sValue,"\"", "''") sValue = string.replace(sValue,",", ";") sValue = "\"" + (sValue) + "\"" aField = aField.lower() #if field is priority_stereo, first write out geometry lon lat list, then priority_stereo if aField == "priority_stereo": partnum = 0 partcount = feat.partcount while partnum < partcount: part = feat.getpart(partnum) part.reset() pnt = part.next() pnt = part.next() # skip first point as it's repreated at the end of the polygon pntTotal = part.count - 1 pnt_count = 0 pTest = 1 if pntTotal > 16: pTest = int(pnt_count / 16.00001) + 1 outPolygon = "\"" while pnt: if (pnt_count % pTest) == 0: #convert to dd Ocentric or Ographic here if og2oc_convert: #show_msg = str(pnt.y); go_tlac(show_msg) y = og2oc(pnt.y, 3396190.0, 3376200.0) elif oc2og_convert: y = oc2og(pnt.y, 3396190.0, 3376200.0) else: y = (pnt.y) outPolygon = outPolygon + str((pnt.x+360) % 360) + " " + str(y) + " " pnt = part.next() pnt_count += 1 #show_msg = "Feature Num: " + str(pnt_count) + " Vertices: " + str(pntTotal); go_tlac(show_msg) #strip off last character and add quote outPolygon = outPolygon[0:(len(outPolygon) -1)] + "\"" sOut = sOut + "%s," % (outPolygon) partnum = partnum + 1 #End Shape loop sOut = sOut + "%s," % (sValue) #strip off last character and add NewLine sOut = sOut[0:(len(sOut) -1)] + "\n" #edited code here, removed sout+ outFile.write(sOut) inRow = inRows.next() outFile.flush() outFile.close() except Exception, ErrorDesc: gp.AddError(ErrorDesc[0]) if outFile: outFile.close() gp.AddError(gp.getmessages(2))