def GetUTMFromRaster(inputRaster): r = arcpy.Raster(inputRaster) x_centre = r.extent.XMin + ( r.extent.XMax - r.extent.XMin) / 2 # center of inpout image (x) y_centre = r.extent.YMin + ( r.extent.YMax - r.extent.YMin) / 2 # center of inpout image (y) input_sr_pcs = arcpy.GetUTMFromLocation(x_centre, y_centre) return input_sr_pcs
centroidY = eval(orderInfo['ORDER_GEOMETRY']['CENTROID'])[0][0][1] if os.path.exists(os.path.join(jobfolder, OrderNumber, 'OrderGeometry.shp')): arcpy.CopyFeatures_management( os.path.join(jobfolder, OrderNumber, 'OrderGeometry.shp'), site_feat) else: OrderGeometry = createGeometry( eval(orderInfo[u'ORDER_GEOMETRY'][u'GEOMETRY'])[0], orderInfo['ORDER_GEOMETRY']['GEOMETRY_TYPE'], scratch, 'OrderGeometry.shp') mxdextent = os.path.join(scratch, 'extent.mxd') shutil.copy(mxdtemplate, mxdextent) mxd = arcpy.mapping.MapDocument(mxdextent) df = arcpy.mapping.ListDataFrames(mxd, '*')[0] if projection == '': arcpy.GetUTMFromLocation(centroidX, centroidY) else: sr = arcpy.SpatialReference(int(FactoryCode)) df.spatialReference = sr geo_lyr = arcpy.mapping.Layer(site_feat) arcpy.mapping.AddLayer(df, geo_lyr, 'TOP') geometry_layer = arcpy.mapping.ListLayers(mxd, 'OrderGeometry', df)[0] geometry_layer.visible = False geo_extent = geometry_layer.getExtent(True) df.extent = geo_extent df.scale = int(scale) print df.scale arcpy.RefreshActiveView() arcpy.mapping.ExportToJPEG(mxd, os.path.join(scratch, 'extent.jpg'),
def export_frame(imagedict, ordergeometry, buffergeometry): for image_year in imagedict.keys(): image_per_year = 0 for image in imagedict[image_year]: image_source = image['IMAGE_SOURCE'] image_collection = image['IMAGE_COLLECTION_TYPE'] auid = str(image['AUI_ID']) imagepath = image['ORIGINAL_IMAGE_PATH'] image_per_year += 1 if FactoryCode == '': sr = arcpy.SpatialReference(4326) elif FactoryCode == 'UTM': sr = arcpy.GetUTMFromLocation(centroidX, centroidY) else: sr = arcpy.SpatialReference(int(FactoryCode)) fin_image_name = os.path.join( job_fin, image_year + '_' + image_source + '_' + str(image_per_year) + '.tif') if image_collection == 'DOQQ': arcpy.overwriteOutput = True mxd = arcpy.mapping.MapDocument(mxdexport_template) df = arcpy.mapping.ListDataFrames(mxd, '*')[0] df.SpatialReference = sr lyrpath = os.path.join(scratch, str(auid) + '.lyr') arcpy.MakeRasterLayer_management(imagepath, lyrpath) image_lyr = arcpy.mapping.Layer(lyrpath) geo_lyr = arcpy.mapping.Layer(ordergeometry) arcpy.mapping.AddLayer(df, geo_lyr, 'TOP') arcpy.mapping.AddLayer(df, image_lyr, 'TOP') image_layer = arcpy.mapping.ListLayers(mxd, "", df)[0] geometry_layer = arcpy.mapping.ListLayers( mxd, 'OrderGeometry', df)[0] geometry_layer.visible = False image_extent = image_layer.getExtent() geo_extent = geometry_layer.getExtent() df.extent = geo_extent print df.scale df.extent = geo_extent if df.scale < 6000: df.scale = 6000 print df.scale df.scale = ( (df.scale / 100) + 1 ) * 100 #very important setting as it defines how much of the image will be displayed to FE w_res = 7140 h_res = int((geo_extent.height / geo_extent.width) * w_res) arcpy.RefreshActiveView() desc = arcpy.Describe(lyrpath) bandcount = desc.bandcount arcpy.env.compression = "LZW" arcpy.env.pyramid = "NONE" #arcpy.Clip_management(imagepath,'%s %s %s %s'%(geo_extent.XMin,geo_extent.YMin,geo_extent.YMax,geo_extent.XMax),os.path.join(job_fin,image_year + '_' + image_source + '_' +str(image_per_year) + '.tif'), maintain_clipping_extent = 'NO_MAINTAIN_EXTENT') #arcpy.Clip_management(imagepath,'%s %s %s %s'%(geo_extent.XMin,geo_extent.YMin,geo_extent.YMax,geo_extent.XMax),os.path.join(job_fin,image_year + '_' + image_source + '_' +str(image_per_year) + '.tif'), maintain_clipping_extent = 'NO_MAINTAIN_EXTENT') arcpy.Clip_management( imagepath, '#', fin_image_name, in_template_dataset=buffergeometry, clipping_geometry="NONE", maintain_clipping_extent='NO_MAINTAIN_EXTENT') #arcpy.ProjectRaster_management(os.path.join(job_fin,image_year + '_' + image_source + '_' +str(image_per_year) + '.tif'),os.path.join(job_fin,image_year + '_' + image_source + '_' +str(image_per_year) + '2.tif'),sr,'BILINEAR') # if bandcount == 1: # arcpy.mapping.ExportToTIFF(mxd,os.path.join(job_fin,image_year + '_' + image_source + '_' +str(image_per_year) + '.tif'),df,df_export_width=w_res,df_export_height=h_res,world_file=True,color_mode = '8-BIT_GRAYSCALE',tiff_compression = 'NONE') # else: # arcpy.mapping.ExportToTIFF(mxd,os.path.join(job_fin,image_year + '_' + image_source + '_' +str(image_per_year) + '.tif'),df,df_export_width=w_res,df_export_height=h_res,world_file=True,color_mode = '24-BIT_TRUE_COLOR',tiff_compression = 'NONE') # arcpy.DefineProjection_management(os.path.join(job_fin,image_year + '_' + image_source + '_' +str(image_per_year) + '.tif'),sr) mxd.saveACopy(os.path.join(scratch, auid + '_export.mxd')) del mxd else: arcpy.env.pyramid = "NONE" img_sr = arcpy.Describe(imagepath).spatialReference arcpy.overwriteOutput = True if img_sr.name == 'Unknown' or img_sr.name == 'GCS_Unknown': arcpy.DefineProjection_management(imagepath, 4326) arcpy.CopyRaster_management(imagepath, fin_image_name, background_value=0, nodata_value=0, transform=True) else: arcpy.ProjectRaster_management(imagepath, fin_image_name, sr, 'CUBIC') arcpy.DefineProjection_management(fin_image_name, sr) set_raster_background(fin_image_name, 'white') raster_desc = arcpy.Describe(fin_image_name) extent = raster_desc.extent centerlong = round(extent.XMin + (extent.XMax - extent.XMin) / 2, 7) centerlat = round(extent.YMin + (extent.YMax - extent.YMin) / 2, 7) set_imagedetail( extent, centerlat, centerlong, image_year + '_' + image_source + '_' + str(image_per_year) + '.tif')
def export_geotiff(imagedict, ordergeometry, image_comment): arcpy.AddMessage("Adding to template: " + str(imagedict)) mxd = arcpy.mapping.MapDocument(mxdexport_template) df = arcpy.mapping.ListDataFrames(mxd, '*')[0] geo_lyr = arcpy.mapping.Layer(ordergeometry) arcpy.mapping.AddLayer(df, geo_lyr, 'TOP') ordered_all_values = imagedict.keys() ordered_all_values.sort() print ordered_all_values for order_value in ordered_all_values: auid = imagedict[order_value][0] image_source = imagedict[order_value][1] imagepath = imagedict[order_value][2] image_background = imagedict[order_value][3].lower() arcpy.SetRasterProperties_management(imagepath, data_type='PROCESSED') set_raster_background(imagepath, image_background) img_sr = arcpy.Describe(imagepath).spatialReference print img_sr.name if img_sr.name == 'Unknown' or img_sr.name == 'GCS_Unknown': arcpy.DefineProjection_management(imagepath, 4326) lyrpath = os.path.join(scratch, str(auid) + '.lyr') arcpy.MakeRasterLayer_management(imagepath, lyrpath) image_lyr = arcpy.mapping.Layer(lyrpath) arcpy.mapping.AddLayer(df, image_lyr, 'TOP') if FactoryCode == '': sr = arcpy.SpatialReference(3857) #web mercator elif FactoryCode == 'UTM': sr = arcpy.GetUTMFromLocation(centroidX, centroidY) else: sr = arcpy.SpatialReference(int(FactoryCode)) df.spatialReference = sr geometry_layer = arcpy.mapping.ListLayers(mxd, 'OrderGeometry', df)[0] geometry_layer.visible = False geo_extent = geometry_layer.getExtent(True) df.extent = geo_extent MapScale = 6000 if PrintScale is not None: df.scale = PrintScale MapScale = PrintScale else: arcpy.AddError('No scale set for order') export_width = 5100 export_height = 6600 arcpy.RefreshActiveView() arcpy.overwriteOutput = True if image_comment != "": report_image_name = image_year + '_' + image_source + '_' + UserMapScale + '_' + image_comment + '.tif' else: report_image_name = image_year + '_' + image_source + '_' + UserMapScale + '.tif' arcpy.AddMessage("Exporting: " + report_image_name) arcpy.env.pyramid = "NONE" arcpy.mapping.ExportToTIFF(mxd, os.path.join(job_fin, report_image_name), df, df_export_width=export_width, df_export_height=export_height, world_file=False, color_mode='24-BIT_TRUE_COLOR', tiff_compression='LZW', geoTIFF_tags=True) arcpy.DefineProjection_management(os.path.join(job_fin, report_image_name), sr) print "projecting" wgs84mxd = arcpy.mapping.MapDocument(wgs84_template) image_report = arcpy.mapping.Layer(os.path.join(job_fin, report_image_name)) df = arcpy.mapping.ListDataFrames(wgs84mxd, '*')[0] arcpy.mapping.AddLayer(df, image_report, 'TOP') imagetodesc = arcpy.mapping.ListLayers(wgs84mxd, '*', df)[0] extent = arcpy.Describe(imagetodesc).extent centerlong = round(extent.XMin + (extent.XMax - extent.XMin) / 2, 7) centerlat = round(extent.YMin + (extent.YMax - extent.YMin) / 2, 7) set_imagedetail(extent, centerlat, centerlong, report_image_name)