Select to view content in your preferred language

Need major, major help converting Tifs into a Mosaic with Python

2983
11
02-26-2020 01:26 PM
JohnPavek
Emerging Contributor
import os, cx_Oracle, string, datetime, time
import arcgisscripting,arcpy
import shutil


class PlateMapGeneration:
pass

user = ''
sdeusr = ''
fldr = ''
path = ''
asset_path = ''
sde = ''
projection = ''
connect_string = ''

timestamp_d = ''
timestamp_t = ''

filelist = []
maplist = []

local_path = ''
final_path = ''
temp_table_conn = ''
sde_conn = ''
plates_gis = ''

def start_process(self, username_, ************_, ************_, sde_, db_):
global user
global *************
global **********
global path
global asset_path
global sde
global connect_string
global timestamp_d
global timestamp_t
global projection
global local_path
global final_path
global sde_conn
global plates_gis

user = username_
*********** = ***************_
********* = *************
path = '\\' + '\\' + '**************\\pythonadmin\\data\\'+*************+'\\plates\\tif\\'

sde = sde_
connect_string = user + '/' + user + '***************' + db_

#local_path = 'C:\\Users\\pythonadmin\\AppData\\Roaming\\ESRI\\Desktop10.3\\ArcCatalog\\' 
#local_path = 'C:\\Python_Cloud_Scripts\\conn\\'
local_path = 'E:\\GIS_MXD\\**************\\PLATES_CAD_GIS1.gdb\\'
final_path = 'E:\\GIS_MXD\\**************\\PLATES_CAD_GIS.gdb\\'
sde_conn = local_path + string.upper(sdeusr) + '.sde\\' + sde
plates_gis = '*******************'

asset_path = '\\' + '\\' + '****************\\pythonadmin\\data\\'+************+'\\assets\\' + plates_gis + '\\'

#print start time
starttime = datetime.datetime.today()
print 'Start Time: ' + str(starttime)

timestamp_d = str(starttime.date())
timestamp_t = str(starttime.time()).replace(':','_')

if string.upper(user) == '*************':
projection = "PROJCS['NAD_1983_StatePlane_New_Jersey_FIPS_2900_Feet',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',492125.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-74.5],PARAMETER['Scale_Factor',0.9999],PARAMETER['Latitude_Of_Origin',38.83333333333334],UNIT['Foot_US',0.3048006096012192]];IsHighPrecision"
elif string.upper(user) == 'CUC_DELMARVA':
projection = "PROJCS['NAD_1983_StatePlane_Delaware_FIPS_0700_Feet',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',656166.6666666665],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-75.41666666666667],PARAMETER['Scale_Factor',0.999995],PARAMETER['Latitude_Of_Origin',38.0],UNIT['Foot_US',0.3048006096012192]];IsHighPrecision"
else:
projection = "PROJCS['WGS_1984_Web_Mercator',GEOGCS['GCS_WGS_1984_Major_Auxiliary_Sphere',DATUM['D_WGS_1984_Major_Auxiliary_Sphere',SPHEROID['WGS_1984_Major_Auxiliary_Sphere',6378137.0,0.0]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],UNIT['Meter',1.0]];IsHighPrecision"

def end_process(self):
endtime = datetime.datetime.today()
print 'End Time: ' + str(endtime)

def MakeFolder(self,**********,gis):
gp = arcgisscripting.create()

folderpath = '\\' + '\\' + '****************\\pythonadmin\\data\\' + ************ + '\\assets\\' + gis + '\\'
if os.path.exists(folderpath) == False:
print 'Folder does not exist'
os.mkdir(folderpath)
os.chdir(folderpath)
os.mkdir(folderpath + 'spatial\\')
gp.CreateFileGDB(folderpath, str(gis)+'.gdb')
else:
print 'Folder already exists'
filelist = []
gp.Delete_management(folderpath+gis+'.gdb')
filelist = os.listdir(folderpath + 'spatial\\')
a=0
while a < len(filelist):
os.remove(folderpath + 'spatial\\'+ filelist[a])
a=a+1
gp.CreateFileGDB(folderpath, str(gis)+'.gdb') 
a=0
print 'Folder creation complete'

def FormatTupleNum(self,num):
numstr = ""
numstr = num
return numstr.lstrip('(').rstrip('),')

def get_filelist(self):
global filelist
global path

filelist = os.listdir(path)

print "Files:"
print filelist

def get_map(self):
global connect_string
global maplist

x=0

con = cx_Oracle.connect(connect_string)
cur = con.cursor()
#maps = 'CC3D'
#cur.execute('select b.name from PLATE_MAPS_DETAIL a, MAP_VIEWS b where A.MAP_NAME = B.NAME and a.map_name = '+'\''+maps+'\'')
cur.execute('select b.name from PLATE_MAPS_DETAIL a, MAP_VIEWS b where A.MAP_NAME = B.NAME order by b.name')
#cur.execute('select map_name from asset_plates_gis order by creation_date')

maplist = cur.fetchall()

while x < len(maplist):
#mapstr = ''
mapstr = self.FormatTupleNum(str(maplist[x]))
maplist[x] = mapstr.replace('\'','')
x=x+1
print 'Maps:'
print maplist 
con.commit()
#con.close()

def create_tfw(self):
global path
global maplist

x=0
y=0

self.get_map()

con = cx_Oracle.connect(connect_string)
cur = con.cursor()

coordnumber = '1.3369988'


cur.execute('delete from ASSET_PLATES')

con.commit()

while x < len(maplist):
mapstr = ''
mapstr = maplist[x]
print mapstr
try:
print path + mapstr +'.tif'
modified_time = time.asctime(time.localtime(os.path.getmtime(path + mapstr +'.tif')))


cur.callproc('pkg_assets.populate_asset_plates',(mapstr,modified_time))

cur.execute('SELECT b.LLX FROM PLATE_MAPS_DETAIL a, MAP_VIEWS b WHERE A.MAP_NAME = B.NAME and a.map_name = '+'\''+mapstr+'\'')
coordllxlist = cur.fetchone()
coord_llx = ''
coord_llx = self.FormatTupleNum(str(coordllxlist[0]))

cur.execute('SELECT B.URY FROM PLATE_MAPS_DETAIL a, MAP_VIEWS b WHERE A.MAP_NAME = B.NAME and a.map_name = '+'\''+mapstr+'\'')
coordurylist = cur.fetchone()
coord_ury = ''
coord_ury = self.FormatTupleNum(str(coordurylist[0]))

f = open(path + maplist[x] + '.tfw','w')
f.write(coordnumber + '\n')
f.write('0' + '\n')
f.write('0' + '\n')
f.write('-'+coordnumber + '\n')
f.write(coord_llx + '\n')
f.write(coord_ury)
print maplist[x] + '.tfw created' 

f.close()
con.commit()
except:
print"Cannot find image " + mapstr
x=x+1
con.commit()
#con.close()

def generate_plate_gis(self):
global user
global maplist
global plates_gis
global local_path
global temp_table_conn
global sde_conn
global connect_string

con = cx_Oracle.connect(connect_string)
cur = con.cursor()

x=0
counter=0

#populate new plates into ASSET_PLATES_GIS
cur.callproc('pkg_assets.get_new_asset_plates')
con.commit()

#self.MakeFolder(user, plates_gis)
print local_path+plates_gis

if arcpy.Exists(local_path+plates_gis) == True:
while x < len(maplist):
mapstr = ''
mapstr = maplist[x]

counter = cur.callfunc('pkg_assets.update_plates',cx_Oracle.NUMBER,[mapstr])
if counter > 0:
print mapstr + ' will be updated.'
try:
sqlexpression = "\"NAME\" = \'" + mapstr + ".tif\' OR " + "\"NAME\" = \'" + mapstr + ".TIF\' OR " +"\"NAME\" = \'" + mapstr + "\'"
print sqlexpression
sttime = datetime.datetime.today()
print 'Start Time: ' + str(sttime)
arcpy.MakeRasterCatalogLayer_management (local_path+plates_gis, mapstr+"_view", sqlexpression, "", "") 
sttime = datetime.datetime.today()
#print 'Make Raster: ' + str(sttime)
#print 'Raster Created'
arcpy.DeleteRasterCatalogItems_management(mapstr+"_view") 
sttime = datetime.datetime.today()
#print 'Deleted Raster: ' + str(sttime)
#print 'Deleted existing Raster Catalog record.'
arcpy.RasterToGeodatabase_conversion(path+mapstr+'.tif',local_path+plates_gis)
#arcpy.RasterToGeodatabase_conversion('C:\Python_Cloud_Scripts\*********\BC1Q.tif',sde_conn+plates_gis) 
sttime = datetime.datetime.today()
#print 'RasterTo Geo: ' + str(sttime)
#print 'RasterToGeodatabase_conversion'
cur.callproc('pkg_assets.asset_plates_gis_createtime',[mapstr])
con.commit()
print mapstr +' added to SDE Raster Catalog.'
except Exception, e:
print e.message
x=x+1

#arcpy.WorkspaceToRasterCatalog_management(asset_path+plates_gis+'.gdb',sde_conn+plates_gis,"NONE","NONE")
else:
print plates_gis + ' does not exist'

con.commit()
#con.close()
filelist = []
files = os.listdir (local_path)
for filename in files:
if filename.endswith(".lock"):
print 'lock'
else:
filelist.append(filename)
fullpath = local_path + filename
shutil.copy(fullpath, final_path)
#shutil.copy(local_path, final_path)

self.end_process()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If needed we could just add to the current code and move it into the mosaic dataset from the GDB.  Any...any...help would be massively appreciated as we are all at our programming limits.

0 Kudos
11 Replies
JohnPavek
Emerging Contributor

Hi David

Here is what I saw and I now know it is a Raster Catalog because the heading of the Properties: box is "Raster Catalog Properties" which I don't why I missed that.  This is one of the reasons I hate coding/programming...you miss a comma and your lost for hours/days/weeks...I don't have the patience for that.

I will look into your suggestion David about the code.  I'm going to try to use Modelbuilder to help me get the code together.  It might be worth doing your suggestion but I have learned if it is working don't break it.  If I just could get the Raster Catalog GDB to convert/be loaded/etc into the mosaic dataset then I could use Modelbuilder to get that to work, export out to Python and add the codeset in getting that to play nice with the other code.

0 Kudos
DavidPike
MVP Notable Contributor

Use Create Referenced Mosaic Dataset. If the files which get removed and replaced in the raster catalog keep the same names, you will only need to create this mosaic dataset once, then it will simply reference the paths in the catalog. Ie create it once and that's it.

0 Kudos