def check_cs(self, imagepath): new = imagepath.replace('mosaic', 'registered') sc = arcpy.Describe(imagepath).spatialReference scnew = arcpy.Describe(new).spatialReference path = os.path.split(new)[0] name = os.path.split(new)[1] if 'rank' in name.lower() and 'jenoptik' in name.lower(): new = glob.glob( os.path.join('D:\\Flight {}', 'registered', '*{}**{}*.tif').format(str(flight), str(fid), 'Jenoptik'))[0] #new = glob.glob(os.path.join(path,'*{}**{}*.tif').format(fid,'Jenoptik')) if 'rank' in name.lower() and 'vnir' in name.lower(): new = glob.glob( os.path.join('D:\\Flight {}', 'registered', '*{}**{}*.tif').format(str(flight), str(fid), 'VNIR'))[0] if 'rank' in name.lower() and 'ids rgb' in name.lower(): new = glob.glob( os.path.join('D:\\Flight {}', 'registered', '*{}**{}*.tif').format(str(flight), str(fid), 'IDS RGB'))[0] if scnew.name == 'Unknown': arcpy.BatchBuildPyramids_management(new) arcpy.DefineProjection_management(new, sc) else: arcpy.BatchBuildPyramids_management(new) #arcpy.MakeRasterLayer_management(new, new) arcpy.DefineProjection_management(new, sc) pass
def define_output_gdb_projection(gdb, coords): env.workspace = gdb message = "Redefining output GDB's projection" say(message) for fc in arcpy.ListFeatureClasses(): arcpy.DefineProjection_management(fc, coords) for raster in arcpy.ListRasters(): arcpy.DefineProjection_management(raster, coords)
def createShp(location): workspace = "./" shapeCursor = 'AccidentLocations.shp' shapeCursor_weekday = 'SevereAccidentsWeekday.shp' shapeCursor_weekend = 'SevereAccidentsWeekend.shp' # These delete the shapefiles if they exist before carrying out the shapefile creation. arcpy.Delete_management(workspace + '/' + shapeCursor) arcpy.Delete_management(workspace + '/' + shapeCursor_weekday) arcpy.Delete_management(workspace + '/' + shapeCursor_weekend) # These create the feature classes of shapefiles. arcpy.CreateFeatureclass_management(workspace, shapeCursor_weekday, "POINT", "", "DISABLED", "DISABLED") arcpy.CreateFeatureclass_management(workspace, shapeCursor_weekend, "POINT", "", "DISABLED", "DISABLED") arcpy.CreateFeatureclass_management(workspace, shapeCursor, "POINT", "", "DISABLED", "DISABLED") # These set the projection to WGS_84. sr = arcpy.SpatialReference(4326) arcpy.DefineProjection_management(workspace + '/' + shapeCursor, sr) arcpy.DefineProjection_management(workspace + '/' + shapeCursor_weekday, sr) arcpy.DefineProjection_management(workspace + '/' + shapeCursor_weekend, sr) # These add the attribute of AccidentLocations.shp. arcpy.AddField_management(shapeCursor, "A_ID", "TEXT") arcpy.AddField_management(shapeCursor, "Vehicles", "TEXT") arcpy.AddField_management(shapeCursor, "Day", "TEXT") arcpy.AddField_management(shapeCursor, "Severe", "TEXT") # These add the attribute of AccidentLocationsWeekday.shp. arcpy.AddField_management(shapeCursor_weekday, "A_ID", "TEXT") arcpy.AddField_management(shapeCursor_weekday, "Vehicles", "TEXT") arcpy.AddField_management(shapeCursor_weekday, "Day", "TEXT") arcpy.AddField_management(shapeCursor_weekday, "Severe", "TEXT") # These add the attribute of AccidentLocationsWeekend.shp. arcpy.AddField_management(shapeCursor_weekend, "A_ID", "TEXT") arcpy.AddField_management(shapeCursor_weekend, "Vehicles", "TEXT") arcpy.AddField_management(shapeCursor_weekend, "Day", "TEXT") arcpy.AddField_management(shapeCursor_weekend, "Severe", "TEXT") # These set up the cursors for insertion of information into the shapefiles. cursor = arcpy.da.InsertCursor(shapeCursor, ["A_Id", "Vehicles", "Day", "Severe", "SHAPE@"]) cursor_weekday = arcpy.da.InsertCursor(shapeCursor_weekday, ["A_Id", "Vehicles", "Day", "Severe", "SHAPE@"]) cursor_weekend = arcpy.da.InsertCursor(shapeCursor_weekend, ["A_Id", "Vehicles", "Day", "Severe", "SHAPE@"]) # This loop inserts the points into shapefiles row by row. for p in location: # This extracts the longitude and latitude of the point. point = arcpy.Point(float(p[3][1]), float(p[3][0])) cursor.insertRow([p[0], str(p[4]), p[1], p[2], point]) if p[2] == 1: if p[1] in ['Saturday', 'Sunday']: cursor_weekend.insertRow([p[0], str(p[4]), p[1], p[2], point]) else: cursor_weekday.insertRow([p[0], str(p[4]), p[1], p[2], point]) del cursor del cursor_weekday del cursor_weekend
def project(DGN_IN, Projection): if Projection == 'North': arcpy.DefineProjection_management(DGN_IN, KSN1) print "projected in Modified State Plane north" arcpy.SpatialReference.createFromFile(self) elif Projection == 'South': arcpy.DefineProjection_management(DGN_IN, KSS1) print "projected in Modified State Plane South" else: print "could not project - enter north or south" return project
def set_order_geometry(order_obj): ### set order geometry layer file if order_obj.geometry.type.lower() in ['point','multipoint']: config.order_geom_lyr_file = config.order_geom_lyr_point elif order_obj.geometry.type.lower() =='polyline': config.order_geom_lyr_file = config.order_geom_lyr_polyline else: #polygon config.order_geom_lyr_file = config.order_geom_lyr_polygon ### calculate order geometry in PCS order_geometry_pcs = order_obj.geometry.projectAs(order_obj.spatial_ref_pcs) arcpy.CopyFeatures_management(order_obj.geometry, config.order_geometry_gcs_shp) arcpy.DefineProjection_management(config.order_geometry_gcs_shp, order_obj.spatial_ref_gcs) arcpy.CopyFeatures_management(order_geometry_pcs, config.order_geometry_pcs_shp) arcpy.DefineProjection_management(config.order_geometry_gcs_shp, order_obj.spatial_ref_pcs)
def execute(self, parameters, messages): """The source code of your tool.""" arcpy.env.overwriteOutput = True arcpy.AddMessage( "Project a directory of rasters into a consistent projection") for param in parameters: arcpy.AddMessage("Parameter: %s = %s" % (param.name, param.valueAsText)) input_directory = parameters[0].valueAsText coordinate_system = parameters[1].valueAsText # Set environment settings env.workspace = input_directory rasterlist = arcpy.ListRasters("*") arcpy.AddMessage("There are " + str(len(rasterlist)) + " rasters to process.") for raster in rasterlist: # Execute RasterToASCII arcpy.AddMessage("Defining projection for " + str(raster) + ".") arcpy.DefineProjection_management(raster, coordinate_system) return
def downsample(city_id): log('Downsampling points for %s', city_id) output_dir = join(DOWNSAMPLE_DIR, str(city_id)) if not exists(output_dir): os.makedirs(output_dir) log('Created %s', output_dir) else: log('%s already exists!', output_dir) samples_shp = join(LATLNGS_SHP_DIR, '%s.shp' % city_id) downsampling_fishnet_poly_shp = join(output_dir, 'downsampling_fishnet.shp') downsampling_fishnet_label_shp = join(output_dir, 'downsampling_fishnet_label.shp') if not exists(downsampling_fishnet_poly_shp): log('Creating fishnet...') desc = arcpy.Describe(samples_shp) arcpy.CreateFishnet_management( downsampling_fishnet_poly_shp, str(desc.extent.lowerLeft), str(desc.extent.XMin) + ' ' + str(desc.extent.YMax + 10), '0.0012', '0.0012', '0', '0', str(desc.extent.upperRight), 'LABELS', '#', 'POLYGON') log('Fishnet creation complete') samples_identity_shp = join(output_dir, 'samples_identity.shp') if not exists(samples_identity_shp): log('Computing identity...') arcpy.Identity_analysis(samples_shp, downsampling_fishnet_poly_shp, samples_identity_shp) log('Identity complete') samples_stats = join(output_dir, 'samples_stats') if not exists(join(output_dir, 'info')): log('Starting summary statistics...') arcpy.Statistics_analysis(samples_identity_shp, samples_stats, [['price', 'MEAN']], 'FID_downsa') log('Summary statistics complete') log('Detecting if join has already been done...') join_done = False fields = arcpy.ListFields(downsampling_fishnet_label_shp) for field in fields: if field.name == 'MEAN_PRICE': join_done = True if not join_done: log('Performing table join on FID:FID_DOWNSA...') arcpy.JoinField_management(downsampling_fishnet_label_shp, 'FID', samples_stats, 'FID_DOWNSA', ['MEAN_PRICE']) log('Table join on FID:FID_DOWNSA done.') log('Defining projection...') arcpy.DefineProjection_management(downsampling_fishnet_label_shp, PROJECTION_FILE) log('FINISHED downsampling %s', city_id) return downsampling_fishnet_label_shp log('======================END==========================')
def define_projection(self, wildcard, sr): logging.info("define projection") try: fc_list = get_path.pathFinder( env_0=self.inputGDB).get_file_path_with_wildcard_from_gdb( wildcard) for fc in fc_list: print("Defining projection for: {}, SR: {}".format( os.path.basename(fc), sr)) arcpy.DefineProjection_management(fc, sr) logging.info(arcpy.GetMessages(0)) except arcpy.ExecuteError: msgs = arcpy.GetMessages(2) arcpy.AddError(msgs) print(msgs) logging.info(msgs) except: tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n" arcpy.AddError(pymsg) arcpy.AddError(msgs) print(pymsg) print(msgs) logging.info(pymsg) logging.info(msgs)
def make_random_values_raster(inRaster): """takes raster and creates random value for each cell """ # calc lower left coordinate ras_obj = arcpy.Raster(inRaster) lower_left = arcpy.Point(ras_obj.extent.XMin, ras_obj.extent.YMin) # Convert Raster to numpy array np_arr = arcpy.RasterToNumPyArray(inRaster, nodata_to_value=0) # replace values of 1 with random value for i, row in enumerate(np_arr): for j, cell in enumerate(row): if cell > 0: np_arr[i][j] = randint(1, 9) #else: # np_arr[i][j] = 0.0 # Convert Array to raster (keep the origin and cellsize the same as the input) newRaster = arcpy.NumPyArrayToRaster(np_arr, lower_left, ras_obj.meanCellHeight, ras_obj.meanCellWidth, value_to_nodata=-9999) # define projection of output # get the coordinate system by describing a feature class coord_sys = arcpy.Describe(inRaster).spatialReference arcpy.DefineProjection_management(newRaster, coord_sys) return newRaster
def shapefile_creator(features, n, wkid): """ Create a shapefile with a list of Polygon objects, aggregate overlaping buildings and project :param n: (int) number of shapefiles to create :param features: (list) List of Polygon objects :param wkid: (int) MTM zone wkid :return (string) path of shapefile """ arcpy.env.overwriteOutput = True cwd = os.getcwd() building_footprint_1 = cwd + r"\output\building_footprint_1_{}.shp".format( n) building_footprint_2 = cwd + r"\output\building_footprint_2_{}.shp".format( n) building_footprint_z21 = cwd + r"\output\building_footprint_z21_{}.shp".format( n) # final shapefile arcpy.CopyFeatures_management(features, building_footprint_1) # project sr = arcpy.SpatialReference(3857) # WGS_1984_Web_Mercator_Auxiliary_Sphere arcpy.DefineProjection_management(building_footprint_1, sr) # Define Projection sr2 = arcpy.SpatialReference(wkid) arcpy.Project_management(building_footprint_1, building_footprint_2, sr2) # Project arcpy.Delete_management(building_footprint_1) # Dissolve overlaping buildings arcpy.Dissolve_management(building_footprint_2, building_footprint_z21, multi_part="SINGLE_PART") #ca.AggregatePolygons(building_footprint_2, building_footprint_z21, 0.01, 3, 3, "ORTHOGONAL", "") arcpy.Delete_management(building_footprint_2) return building_footprint_z21
def pairwiseRasterToSave(array_input, lower_left_corner, dsc, spatial_ref, pairwise_output_folder, inputrastername1, inputrastername2, calculationtype, score, matrixinput, dictpairwiserastername, sum_of_all_cells, listpairwiserastername, tableOption3IsChecked, somePolygonsIsTrue, numbOfSome, tooloption): if (numbOfSome != 0): pairwise_marine_use_array = np.copy(array_input) pairwise_marine_use_array = pairwise_marine_use_array.astype(int) pairwise_marine_use_array[pairwise_marine_use_array!=0.] = 1. pairwiseRaster = arcpy.NumPyArrayToRaster(pairwise_marine_use_array, lower_left_corner, dsc.meanCellWidth, dsc.meanCellHeight, 0) gf.createNewPath(pairwise_output_folder) raster_outputname = gf.returnRasterFilename1(inputrastername1, inputrastername2, calculationtype) pairwiseRaster.save(pairwise_output_folder+"\\"+raster_outputname) arcpy.DefineProjection_management(pairwise_output_folder+"\\"+raster_outputname, spatial_ref) # if it is chosen to only add polygons to map with highest and lowest scores: if ((tableOption3IsChecked == "true") and (somePolygonsIsTrue == "true") and (numbOfSome != "all")): if sum_of_all_cells in dictpairwiserastername: dictpairwiserastername[sum_of_all_cells].append(raster_outputname) if (tooloption == "producescoremap"): dictpairwiserastername[sum_of_all_cells].append(score) dictpairwiserastername[sum_of_all_cells].append(matrixinput) else: if (tooloption == "producescoremap"): dictpairwiserastername[sum_of_all_cells] = [raster_outputname, score, matrixinput] elif (tooloption == "producecountmap"): dictpairwiserastername[sum_of_all_cells] = [raster_outputname, matrixinput] # if it is chosen to add all polygons to map: else: listpairwiserastername.append(raster_outputname) if (tooloption == "producescoremap"): listpairwiserastername.append(score) listpairwiserastername.append(matrixinput) return (dictpairwiserastername, listpairwiserastername)
def define_projection(file_list): """ Give raster(s) proper MODIS sinusoidal projection metadata. Some MODIS data does not have an adequately defined projection for some software like arcmap to use :param file_list: a list of one or more filepaths """ # accept list of file_list file_list = raster.enf_rastlist(file_list) # custom text for MODIS sinusoidal projection proj = """PROJCS["Sinusoidal", GEOGCS["GCS_Undefined", DATUM["D_Undefined", SPHEROID["User_Defined_Spheroid",6371007.181,0.0]], PRIMEM["Greenwich",0.0], UNIT["Degree",0.017453292519943295]], PROJECTION["Sinusoidal"], PARAMETER["False_Easting",0.0], PARAMETER["False_Northing",0.0], PARAMETER["Central_Meridian",0.0], UNIT["Meter",1.0]]""" for filename in file_list: arcpy.DefineProjection_management(filename, proj) return
def define_swe_projection(): fdir = r'D:\project05\GLOBSWE\tif\SWE_avg\\' fdir = r'D:\project05\GLOBSWE\tif\SWE_max\\' for f in os.listdir(fdir): print f sr = arcpy.SpatialReference("North Pole Lambert Azimuthal Equal Area") arcpy.DefineProjection_management(fdir + f, sr)
def create_boundary(NAME, meter): input_path = os.path.abspath("input_folder") output_path = os.path.abspath("output_folder/shp_files") if os.path.isdir("output_folder/"): rmtree('output_folder/') os.mkdir("output_folder/") os.mkdir("output_folder/shp_files") else: os.mkdir("output_folder/") os.mkdir("output_folder/shp_files") asc_file_path = os.path.join(input_path, NAME + ".asc") prj_file_path = os.path.join(input_path, NAME + ".prj") boundary = os.path.join(output_path, "{}_boundary.shp".format(NAME)) bound_neg2m = os.path.join(output_path, "{}_bound_neg2m.shp".format(NAME)) bound_rec = os.path.join(output_path, "{}_bound_rec.shp".format(NAME)) arcpy.DefineProjection_management(asc_file_path, prj_file_path) reclassified = arcpy.sa.Reclassify( asc_file_path, "VALUE", "0 10000 1") #"101.102745 105.156837 1;105.156837 108.940979 1") arcpy.RasterToPolygon_conversion(reclassified, boundary, "NO_SIMPLIFY") arcpy.Buffer_analysis(boundary, bound_neg2m, "{} Meters".format(meter), "FULL", "ROUND", "NONE", "", "PLANAR") # require user input for meters arcpy.MinimumBoundingGeometry_management(bound_neg2m, bound_rec, "RECTANGLE_BY_AREA", "NONE", "", "NO_MBG_FIELDS")
def filter_and_project_raster(state, ras, yr): odir = constants_lu.epic_dir + os.sep + state filtered_state = odir + os.sep + 'open_' + str(yr) + '_' + state tmp_ras = odir + os.sep + 'tmp_' + str(yr) + '_' + state try: where = "COUNT > " + str(constants_lu.FILTER_SIZE) att_extract = ExtractByAttributes(ras, where) att_extract.save(tmp_ras) except: logging.info(arcpy.GetMessages()) try: out_set_null = SetNull( Lookup(RegionGroup(tmp_ras, "", "", "NO_LINK", ""), "Count") <= constants_lu.FILTER_SIZE, tmp_ras) out_set_null.save(filtered_state) # Reproject dsc = arcpy.Describe(ras) coord_sys = dsc.spatialReference arcpy.DefineProjection_management(filtered_state, coord_sys) except: logging.info(arcpy.GetMessages()) logging.info('\t Filtering small pixels from state ' + state) return filtered_state
def define_pro_wgs84(in_path): """ Define shpfile project to wgs84 :param in_path: input file path :return: """ print "*****************************************" print "Bat define project to shapeFile----------" assert isinstance(in_path, basestring) if not os.path.exists(in_path): print 'Input path is incorrect ,please re-input.' sys.exit(1) arcpy.env.workspace = in_path for shp in arcpy.ListFiles('*.shp'): wgs84 = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]]," \ "PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]" in_data_set = shp try: arcpy.DefineProjection_management(in_data_set, wgs84) print '%s Define Project wgs84 Success !' % in_data_set except Exception as e: print e.message print "ALL data define project success ---------" print "*****************************************"
def __defineProjection(self, item): try: try: sr = arcpy.Describe(item).spatialReference.name except: print("error getting spatialReference", item) return if not self.fromsrname: self.availablespatialRef.add(sr) return if (sr.lower() == self.fromsrname.lower()): if (self.tosr != None): try: print("DefineProj to", self.tosr.name) print("BEFORE", arcpy.Describe(item).spatialReference.name) arcpy.DefineProjection_management(item, self.tosr) print("AFTER", arcpy.Describe(item).spatialReference.name) print("sucessfully Defined", item) except: print("error Defining spatialReference", item) else: print(item, sr) except: tb = traceback.format_exc() print(tb)
def createShape(year, accident_location): workspace = "./" shapeCursor = 'AccidentLocations_' + year + '.shp' # This deletes the shapefile if it exists before carrying out the shapefile creation. arcpy.Delete_management(workspace + '/' + shapeCursor) # This creates the feature classes of the shapefile. arcpy.CreateFeatureclass_management(workspace, shapeCursor, "POINT", "", "DISABLED", "DISABLED") # This sets the projection to WGS_84. sr = arcpy.SpatialReference(4326) arcpy.DefineProjection_management(workspace + '/' + shapeCursor, sr) # This adds the attribute names into the shapefile. arcpy.AddField_management(shapeCursor, "A_ID", "TEXT") arcpy.AddField_management(shapeCursor, "YEAR", "TEXT") # This creates the cursor that writes points. cursor = arcpy.da.InsertCursor(shapeCursor, ["A_Id", "YEAR", "SHAPE@"]) # This loop writes the points row by row into the shapefile. for p in accident_location: if p[1] == year: # This extracts the longitude and latitude of the point. point = arcpy.Point(float(p[2][1]), float(p[2][0])) cursor.insertRow([p[0], p[1], point]) del cursor
def quantilize(raster, numQuantiles): # try: if extractMask is not None: dir, file = os.path.split(raster) fileBase, ext = os.path.splitext(file) print "Extracting by mask file" extractRasFN = fileBase + 'clp.tif' extractRasPath = os.path.join(dir, extractRasFN) outRas = arcpy.sa.ExtractByMask(raster, extractMask) outRas.save(extractRasPath) # delete_data(os.path.join(outputDir,mosFN+'.tif')) raster = extractRasPath if raster is not None: inArray = arcpy.RasterToNumPyArray(raster, "#", "#", "#", -9999) interval = 100.0 / numQuantiles breakSequence = seq(interval, 100 - interval, interval) quantileArray = npy.zeros_like(inArray, dtype='int32') quantileArray = npy.where(inArray > 0, numQuantiles, quantileArray) inVector = inArray.flatten() ind = npy.where(inVector > 0) inVector = inVector[ind] #Eliminate nodata and zeros # quantilize if len(inVector) == 0: print 'Array is all zero. No results to process.' return print '\nPartitioning ' + str( len(inVector)) + ' non-zero values into ' + str( numQuantiles) + ' quantiles.' quantileBreaks = npy.percentile(inVector, breakSequence) # fixme: consider replacing below with an arcpy reclassify command. Would be faster and take less memory. # Not critical and remap table is fussy- can't be a string for i in range(numQuantiles - 2, -1, -1): quantileArray = npy.where(inArray < quantileBreaks[i], i + 1, quantileArray) quantileArray = npy.where(inArray < 0, -9999, quantileArray) quantileArray = npy.where(inArray == 0, 0, quantileArray) del inArray descData = arcpy.Describe(raster) cellSize = descData.meanCellHeight extent = descData.Extent spatialReference = descData.spatialReference pnt = arcpy.Point(extent.XMin, extent.YMin) quantileRaster = arcpy.NumPyArrayToRaster(quantileArray, pnt, cellSize, cellSize, -9999) arcpy.DefineProjection_management(quantileRaster, spatialReference) dir, file = os.path.split(raster) outPath = os.path.join(dir, 'p' + file) quantileRaster.save(outPath) print 'Saved to:\n', outPath
def rainydays(tiffolder, threshold, rainydayFolder): print("start processing rainy data........ ") sr = arcpy.SpatialReference(4326) tifdata = [] rainydata = [] for tdata in os.listdir(tiffolder): if tdata.endswith(".tif") or tdata.endswith(".tiff"): parseString = tdata.split('.') parse = parseString[4] tifdate = parse[0:8] tifdata.append(tifdate) for rdata in os.listdir(rainydayFolder): if rdata.endswith(".tif") or rdata.endswith(".tiff"): parseStringtdata = rdata.split('.') rainydate = parseStringtdata[1] rainydata.append(rainydate) for i in tifdata: print("checking rainday data for date " + i) if i not in rainydata: print("rainday data for date " + i + " has not been calculated") print("calculating rainday for date " + i) tifname = 'idn_DAY-L.MS.MRG.3IMERG.{0}-S000000-E235959.tif'.format( i) rainyfilename = 'raindays.{0}.threshold_{1}mm.tif'.format( i, threshold) tiffile = os.path.join(tiffolder, tifname) arcpy.CheckOutExtension("spatial") outCon = Con(Raster(tiffile) > int(threshold), 1, 0) outCon.save(os.path.join(rainydayFolder, rainyfilename)) arcpy.DefineProjection_management( os.path.join(rainydayFolder, rainyfilename), sr) print("file " + rainyfilename + " is created") arcpy.CheckInExtension("spatial") print("processing rainy days for threshold " + str(threshold) + " is completed--------")
def convert_dat_to_asc(outdir, file_list): coor_system = arcpy.SpatialReference("British National Grid") arcpy.env.cartographicCoordinateSystem = coor_system arcpy.env.outputCoordinateSystem = coor_system arcpy.env.compression = "LZ77" for name in file_list: fname = os.path.basename(name) fname = fname[:-3] + 'asc' # print(fname) print('coverting file to tif: {0}'.format(fname)) file_id = open(name, 'rb') a = nimrod.Nimrod(file_id) os.chdir(outdir) a.extract_asc(open(fname, 'w')) # print('defining Coord Ref System') asc_name = os.path.join(outdir, fname) saveras_path = os.path.join(outdir, fname[:-3] + 'tif') # load_ras = arcpy.Raster(asc_name) # load_ras.save(saveras_path) arcpy.ASCIIToRaster_conversion(asc_name, saveras_path, data_type="INTEGER") arcpy.DefineProjection_management(saveras_path, coor_system) os.remove(asc_name)
def execute_DSLD(_lastdate, _tiffolder, _DSLD_folder, threshold): sr = arcpy.SpatialReference(4326) date_formatted = date(int(_lastdate[0:4]), int(_lastdate[4:6]), int(_lastdate[6:8])) last_dsldname = 'cli_chirps_dsld_{0}'.format(_lastdate) last_dsldfile = os.path.join(_DSLD_folder, last_dsldname) next_dailyname = 'chirps-v2.0.{0}.{1}.{2}.tif'.format( _lastdate[0:4], _lastdate[4:6], _lastdate[6:8]) next_dailydata = os.path.join(_tiffolder, next_dailyname) if arcpy.Exists(next_dailydata): print("next daily data is available...") print("start processing next DSLD...") new_dsld_date = date_formatted + timedelta(days=1) DSLDYear1 = str(new_dsld_date.year) DSLDmonth1 = str(new_dsld_date.month) DSLDday1 = str(new_dsld_date.day) new_dsld_name = 'cli_chirps_dsld_{0}{1}{2}.tif'.format( DSLDYear1.zfill(4), DSLDmonth1.zfill(2), DSLDday1.zfill(2)) print("Processing DSLD from " + last_dsldfile + " and " + next_dailydata) arcpy.CheckOutExtension("spatial") outDSLDCon = Con( Raster(next_dailydata) > int(threshold), Raster(last_dsldfile) + 1, 0) outDSLDCon.save(os.path.join(_DSLD_folder, new_dsld_name)) arcpy.DefineProjection_management( os.path.join(_DSLD_folder, new_dsld_name), sr) arcpy.CheckInExtension("spatial") print("DSLD File " + new_dsld_name + " is created") else: print("next daily data is not available. Exit...")
def execute(in_netcdf, out_feat, levels=(20, 25, 30, 35, 40, 45), mask=None): # This is a very stupid fix for multiprocessing # But I am sure why arcpy.CheckoutExtension works arcpy.env.overwriteOutput = True arcpy.env.workspace = "in_memory" workspace = "in_memory" # Set all filenames temp_dir = os.path.dirname(os.path.abspath(in_netcdf)) # Emm, csv and img must be saved with .nc layer1 = relocate(in_netcdf, temp_dir, ".img") if not os.path.exists(layer1): fn_csv = relocate(in_netcdf, temp_dir, ".csv") cnt_dir = os.path.dirname(in_netcdf) + "\\cnt" # UnComment this to skip existed results if os.path.exists(relocate(fn_csv, cnt_dir, ".shp")): print("%s existed. Skip!" % relocate(fn_csv, cnt_dir, ".shp")) return ds = netCDF4.Dataset(in_netcdf) # Level 7 == 3.5km refl_l = numpy.ravel(ds.variables["WNDSPD_850MB"]) lon_l = numpy.ravel(ds.variables["lon"]) lat_l = numpy.ravel(ds.variables["lat"]) lon_l, lat_l = utils.projFunc(lon_l, lat_l) print(fn_csv) if not os.path.exists(fn_csv): f_csv = open(fn_csv, "w") f_csv.write("Id,X,Y,Reflect\n") for i in range(refl_l.shape[0]): if not refl_l[i] >= 10: continue refl = refl_l[i] lat = lat_l[i] lon = lon_l[i] f_csv.write("%d,%f,%f,%f\n" % (i, lon, lat, refl)) f_csv.close() print("NC to CSV:", fn_csv) else: print("Have CSV:", fn_csv) reflect = arcpy.CreateUniqueName(arcpy.ValidateTableName("reflect.shp"), workspace) arcpy.MakeXYEventLayer_management(fn_csv, 'X', 'Y', reflect, utils.spatialRef, "Reflect") arcpy.PointToRaster_conversion(reflect, "Reflect", layer1, cellsize=utils.resolution) arcpy.DefineProjection_management(layer1, utils.spatialRef) print("CSV to Rsater:", layer1) # Apply mask on if provided #if mask is not None: # l2 = arcpy.sa.ExtractByMask(in_netcdf, mask) # l21 = arcpy.sa.Con(l2, l2, 0, "VALUE >= 10") #else: # layer1 = in_netcdf # l21 = arcpy.sa.Con(layer1, layer1, 0, "VALUE >= 10") l22 = arcpy.sa.Con(arcpy.sa.IsNull(layer1), 0, layer1) arcpy.sa.ContourList(l22, out_feat, levels) print("Raster to Contour:", out_feat)
def albersOffset(dataset, offset_degs): try: # set local variables dsc = arcpy.Describe(dataset) # get the coordinate system by describing a feature class coord_sys = dsc.spatialReference # Impossible # coord_sys.centralMeridian = 105 #offset_degs crs = re.sub('PARAMETER\[\'central_meridian\'\,.+?]', 'PARAMETER\[\'central_meridian\',' + offset_degs + ']', coord_sys.exportToString()) # print(crs) coord_sys.loadFromString(crs) # print(coord_sys, coord_sys.centralMeridian, offset_degs) # run the tool arcpy.DefineProjection_management(dataset, coord_sys) # print messages when the tool runs successfully print(arcpy.GetMessages(0)) except arcpy.ExecuteError: print(arcpy.GetMessages(2)) except Exception as ex: print(ex.args[0])
def main(): try: # Input input = "../orig/glwd_1.shp" projection = arcpy.SpatialReference( 4326 ) # WGS 1984. See http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/pdf/geographic_coordinate_systems.pdf # Output output = "../data/glwd1.shp" # Process print "Copying the input file, to avoid the next tool from overwriting the original file" arcpy.CopyFeatures_management(input, output) print "Assigning the projection" arcpy.DefineProjection_management(output, projection) print "All done." # Return geoprocessing specific errors except arcpy.ExecuteError: print arcpy.GetMessages() # Return any other type of error except: print "There is non-geoprocessing error." # Check in extensions finally: arcpy.CheckInExtension("spatial")
def main(): try: # Input input = "../orig/suit" projection = arcpy.SpatialReference( 4326 ) # WGS 1984. See http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/pdf/geographic_coordinate_systems.pdf # Output output = "../data/suit.tif" # Process print "Converting into TIFF raster" arcpy.CopyRaster_management(input, output) print "Assigning the projection" arcpy.DefineProjection_management(output, projection) print "All done." # Return geoprocessing specific errors except arcpy.ExecuteError: print arcpy.GetMessages() # Return any other type of error except: print "There is non-geoprocessing error." # Check in extensions finally: arcpy.CheckInExtension("spatial")
def pointGrid(fc, outDir, unitCoversionRatio, gridSize): #dissolved_urban_extent=outDir+"/dissolved_urban_extent.shp" #arcpy.Dissolve_management(fc,dissolved_urban_extent,"#","#","MULTI_PART") fileName = os.path.basename(fc) dscObj = arcpy.Describe(fc) refExtent = dscObj.extent Y_Max = refExtent.YMax X_Max = refExtent.XMax Y_Min = refExtent.YMin X_Min = refExtent.XMin Y_length = (Y_Max - Y_Min) X_length = (X_Max - X_Min) No_Cols = X_length / gridSize No_Rows = Y_length / gridSize dsc = arcpy.Describe(outDir) if dsc.dataType == "Workspace": fishnet = outDir + "/fishnet" fishnet_label = outDir + "/fishnet_label" sample_point = outDir + "/{0}_sample_point".format(fileName) elif dsc.dataType == "Folder": fishnet = outDir + "/fishnet.shp" fishnet_label = outDir + "/fishnet_label.shp" sample_point = outDir + "/{0}_sample_point.shp".format(fileName) arcpy.CreateFishnet_management(fishnet,"{0} {1}".format(X_Min,Y_Min),\ "{0} {1}".format(X_Min,Y_Max),(gridSize),(gridSize),\ No_Rows,No_Cols,"#","LABELS","#","POLYGON") spReference = dscObj.spatialReference arcpy.DefineProjection_management(fishnet_label, spReference) arcpy.Clip_analysis(fishnet_label, fc, sample_point)
def Define_MODIS_Projection(filename): #-------------------------------------------------------------------------------------- # Simple function to give a MODIS file a defined projection for its custom Sinusoidal #-------------------------------------------------------------------------------------- # st up arcpy import arcpy arcpy.env.overwriteOutput = True # custom text for MODIS sinusoidal projection proj= """PROJCS["Sinusoidal", GEOGCS["GCS_Undefined", DATUM["D_Undefined", SPHEROID["User_Defined_Spheroid",6371007.181,0.0]], PRIMEM["Greenwich",0.0], UNIT["Degree",0.017453292519943295]], PROJECTION["Sinusoidal"], PARAMETER["False_Easting",0.0], PARAMETER["False_Northing",0.0], PARAMETER["Central_Meridian",0.0], UNIT["Meter",1.0]]""" arcpy.DefineProjection_management(filename,proj) return
def execute_first_DSLD(_tiffolder, _DSLDFolder, threshold): sr = arcpy.SpatialReference(4326) print("looking at the first daily rainfall data in tif folder...") daily_list = create_daily_List(_tiffolder) first_date = min(daily_list) print("execute first rainy data from date " + first_date) first_data_name = 'chirps-v2.0.{0}.{1}.{2}.tif'.format( first_date[0:4], first_date[4:6], first_date[6:8]) first_daily_data = os.path.join(_tiffolder, first_data_name) daily_Date = date(int(first_date[0:4]), int(first_date[4:6]), int(first_date[6:8])) dsld_date = daily_Date + timedelta(days=1) print("creating dsld data " + str(dsld_date) + " from daily rainfall data from " + str(daily_Date)) DSLDYear = str(dsld_date.year) DSLDmonth = str(dsld_date.month) DSLDday = str(dsld_date.day) print(str(dsld_date)) DSLDFilename = 'cli_chirps_dsld_{0}{1}{2}.tif'.format( DSLDYear.zfill(4), DSLDmonth.zfill(2), DSLDday.zfill(2)) print("Processing " + DSLDFilename) arcpy.CheckOutExtension("spatial") outCon = Con(Raster(first_daily_data) > int(threshold), 1, 0) outCon.save(os.path.join(_DSLDFolder, DSLDFilename)) arcpy.DefineProjection_management(os.path.join(_DSLDFolder, DSLDFilename), sr) arcpy.CheckInExtension("spatial") print("file " + DSLDFilename + " is created")
def define_projection(filenames): """ Give raster(s) proper MODIS sinusoidal projection metadata filename may be either a single filepath string or a list of them. """ # accept list of filenames filenames = raster.enf_rastlist(filenames) # custom text for MODIS sinusoidal projection proj = """PROJCS["Sinusoidal", GEOGCS["GCS_Undefined", DATUM["D_Undefined", SPHEROID["User_Defined_Spheroid",6371007.181,0.0]], PRIMEM["Greenwich",0.0], UNIT["Degree",0.017453292519943295]], PROJECTION["Sinusoidal"], PARAMETER["False_Easting",0.0], PARAMETER["False_Northing",0.0], PARAMETER["Central_Meridian",0.0], UNIT["Meter",1.0]]""" for filename in filenames: arcpy.DefineProjection_management(filename, proj) return