This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """ | |
| Created on Wed Sep 9 16:46:15 2020 | |
| @author: mweber | |
| """ | |
| # Combine hydro-region tables if desired | |
| import pandas as pd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Create linestring | |
| line <- st_as_sfc(c("LINESTRING(0 0 , 0.5 1 , 1 1 , 1 0.3)")) %>% | |
| st_sf(ID = "poly1") | |
| #Convert each vertex to point | |
| pt <- st_cast(line, "POINT") | |
| # Grab start or end point: | |
| start <- pt[1,] | |
| end <- pt[nrow(pt),] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(sf) | |
| library(dplyr) | |
| library(mapview) | |
| library(leaflet) | |
| m <- mapview() | |
| m@map = m@map %>% addWMSTiles(group = 'NHDPlus', | |
| "https://watersgeo.epa.gov/arcgis/services/NHDPlus_NP21/NHDSnapshot_NP21/MapServer/WmsServer?", | |
| layers = 4, | |
| options = WMSTileOptions(format = "image/png", transparent = TRUE), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| raster_2d = 2-dimensional array imported with gdal | |
| nodata = nodata value from gdal import for 2-d array | |
| geotransform = geotrasform from gdal import of 2-d array | |
| def raster_to_point(raster_2d, nodata, geotransform): | |
| raster_2d = raster_2d.flatten() | |
| locs = np.where(raster_2d <> nodata)[0] #Get locs in array w/ data | |
| raster_2d = raster_2d[locs] #Reduce data to those locs | |
| geo = list(geotransform) #Make geotransform from raster a list | |
| x = locs % xsize #Convert flattened locations to x coord |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os, arcpy, sys | |
| shp = 'F:/shapefile.shp' | |
| arcpy.MakeFeatureLayer_management(shp,"lyr") | |
| rows = arcpy.SearchCursor("lyr") | |
| for row in rows: | |
| name=str(row.getValue("ColumnName")) | |
| arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", "'ColumnName' = '%s'"%name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library("Lahman") | |
| # Aggregation | |
| Batting %>% | |
| group_by(playerID) %>% | |
| summarize(most_team = names(which.max(table(teamID, exclude=NULL)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import geopandas as gpd | |
| import rasterio | |
| from rasterio.mask import mask | |
| from rasterio.plot import show | |
| from shapely.geometry import mapping | |
| import matplotlib.pyplot as plt | |
| df = gpd.read_file('myfile.shp') | |
| df = df.loc[df['Field']=='SomeValue'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(RCurl) | |
| library(stringr) | |
| url <- "https://gaftp.epa.gov/EPADataCommons/ORD/NHDPlusLandscapeAttributes/StreamCat/States/" # can use hydro dir here instead | |
| filenames <- getURL(url, userpwd="", ftp.use.epsv = FALSE, verbose=FALSE,dirlistonly = TRUE, ssl.verifypeer = FALSE) | |
| destnames <- strsplit(filenames, "\r*\n")[[1]] | |
| destnames = destnames[grep("_OR.zip", destnames)] # different state - or hydroregion if in hydroregion directory | |
| setwd('somedir/subdir') # where you want to download | |
| for (d in destnames){ | |
| d <- str_match_all(d, "href=\"(.*?)\"") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| path = 'some/path' | |
| [os.rename(f, f.replace('some_text','new_text')) for f in os.listdir(path) if f.count('text_to_look_for')] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| df = pd.DataFrame({'year' : pd.Series([1961, 1961, 1961, 1962, 1962,1963, 1963]), | |
| 'value' : pd.Series([0.4, 0.5, 0.2, 0.8, 0.9, 0.8, 0.7])}) | |
| df['max_values'] = pd.Series(df.groupby('year').value.max().values) |