Problem reprojecting a shapefile with st_transform - reprojection-error

I have a few rasters (CRS 32632) and a shapefile with the coordinates of points that I need to study (CRS 2154). I need to crop my rasters with the shape to only keep the area where my points are, but therefore I need to reproject the shapefile in CRS 32632.
Here's my code
data <- st_read("inventory.shp")
st_transform (data, 32632)
This gives me the following error :
Warning message:
In showSRID(uprojargs, format = "PROJ", multiline = "NO") :
Discarded datum Unknown based on GRS80 ellipsoid in CRS definition,
but +towgs84= values preserved
Does anyone have a clue about how I could settle this?

Related

Unusual Mesh Outline PColorMesh

I am utilizing the pcolormesh function in Matplotlib to plot a series of gridded data (in parallel) across multiple map domains. The code snippet relevant to this question is as follows:
im = ax2.pcolormesh(xgrid, ygrid, data.variable.data[0], cmap=cmap, norm=norm, alpha=0.90, facecolor=None)
Where: xgrid = array of longitude points, ygrid = array of latitude points, data.variable.data[0] = array of corresponding data values, cmap = defined colormap, & norm = defined value normalization
Consider the following image generated from the provided code:
The undesired result I've found in the image above is what appears to be outlines around each grid square, or perhaps better described as patchwork that stands out slightly as the mesh alpha is reduced below 1.
I've set facecolor=None assuming that would remove these outlines, to no avail. What additions or corrections can I make to remove this feature?

How to fill a line in 2D image along a given radius with the data in a given line image?

I want to fill a 2D image along its polar radius, the data are stored in a image where each row or column corresponds to the radius in target image. How can I fill the target image efficiently? Such as with iradius or some functions? I do not prefer a pix-pix operation.
Are you looking for something like this?
number maxR = 100
image rValues := realimage("I(r)",4,maxR)
rValues = 10 + trunc(100*random())
image plot :=realimage("Ring",4,2*maxR,2*maxR)
rValues.ShowImage()
plot.ShowImage()
plot = rValues.warp(iradius,0)
You might also want to check out the relevant example code from the F1 help documentation of GMS itself:
Explaining warp a bit:
plot = rValues.warp(iradius,0)
Assigns values to plot based on a value-lookup in rValues.
For each pixel in plot a coordinate position in rValues is computed, and the value is simply looked up. If the computed coordinate is non-integer, bilinear interpolation between the 4 closest points is used.
In the example, the two 'formulas' for the coordinate calculation are simple x' = iradius and y' = 0 where iradius is an expression computed from the coordinate in plot, for convenience.
You can feed any expression into the parameters for warp( ) and the command is closely related to just using the square bracket notation of addressing values. In fact, the only difference is that warp performs the bilinear interpolation of values instead of truncating the coordinates to integer values.

Rotating series of polygons aswell as the envelope

I have a polygon (rectangle or very close it) in a geopandas dataframe that is at an angle relative the x-axis, i.e. it is neither horizontal not vertical. I have a function that splits polygons into smaller rectangles (isometric) but it only works (as desired) on polygon making an angle that is a multiple of pi/2 with the x-axis.
So, my idea has been to rotate any polygon that does not satisfy my requirements, split it and rotate it back to its original position.
For instance:
polygon =
id geometry
85 POLYGON ((49.37794 51.395203, 49.37794 51.395203, 49.37794 51.395203, 49.37794 51.395203, 49.178337 50.363914, 49.178337 50.363914, 49.178337 50.363914, 49.178337 50.363914, 59.99021 48.733814, 59.99021 48.733814, 59.99021 48.733814, 59.99021 48.733814, 60.223083 49.698566, 60.223083 49.698566, 60.223083 49.698566, 60.223083 49.698566, 49.37794 51.395203))
which looks like this:
Now, I determine its angle with the x-axis and rotate it:
polygon = pd.DataFrame(geostore_obstacles_geometry_polygon.loc[85:85,])
polygon['angle'] = polygon.apply(lambda row : polygon_angle(row['geometry']), axis = 1)
polygon = gpd.GeoDataFrame(polygon)
polygon = polygon.set_geometry('geometry')
polygon['rotated'] = polygon.apply(lambda row : shapely.affinity.rotate(row['geometry'], row['angle']), axis = 1)
polygon = polygon.set_geometry('rotated')
which gives:
This step splits the polygon inte smaller pieces:
polygon['add'] = polygon.apply(lambda row : split_polygon_up(row['rotated'],side_length=side_length, shape="square", thresh=threshold), axis = 1)
polygon = polygon.explode('add')
polygon = polygon.set_geometry('add')
Before I finally rotate it back
polygon['rotated_add'] = polygon.apply(lambda row : shapely.rotate(row['add'], -row['angle']), axis = 1)
polygon = polygon.set_geometry('rotated_add')
But, as you can imagine, this is not what I expect to have (sorry for the very uggly image).
I understand WHY it does this but I cannot solve it. I have some ideas that the one possible solution would be to rotate all the smaller polygons together with the convex hull or envelope of their union, but I struggle using geopandas to do it.
I would be immensely grateful for any help on how to solve this issue. The dataframe obtained after all the transformations can be found here: https://drive.google.com/file/d/1wY7g3jsD7PNpaTkGBjbGvYArpRUr0UIk/view?usp=sharing
The relevant function shapely.rotate() has origin='center' as its default option. To rotate around a particular point (x,y), you must specify explicitly with origin=(x,y).
In your particular case, the centroid of the original polygon is a good choice for (x,y).

rDGAL, Tiff Files, and WorldFile

I have a set of tiff files that display convective weather across the continental US (NAD83 projection) in pixel locations from Iowa State University. My goal is the transformation of the pixel locations to lat/lon data. I read in the tiff file data as a SpatialGridDataFrame with...
imageData = readGDAL( fileNameDir, silent = TRUE )
I read somewhere that readGDAL will seek a World File if no projection data exist in the tiff file, so I created such a file (nad83WorldFile.wld) with the requisite information, see info at ESRI. I put the wld file in the same directory as my R scripts. The coefficients for the wld file are:
A = 0.01
B = 0.0
C = 0.0
D = -0.01
E = -126.0
F = 50.0
I seek advice and guidance on the pixel-to-lat/lon projection. A data file for the readGDAL example of fileNameDir and documentation on the World File format are provided in the hypertext links above. I had to change the file extension from *.png to *.tiff.
Normally, if you know that your data are projected, but that this projection isn't part of your tif file, your can simply add it in your R object after the import:
proj4string(imageData) <- CRS("your projection")
I like using EPSG for that, if your tif was in GoogleEarth projection for example I would do:
proj4string(imageData) <- CRS("+init=EPSG:4326")
Just find what you NAD83 exact projection is (this site can help http://spatialreference.org/).
Then you can reproject it in the your choice of projection:
imageDataProj <- spTransform(imageDataProj, CRS("your new projection"))
As a side note, I always prefer using the raster package for handling raster formats. However, changing the projection of a big raster file with R can be fastidious, so now I use GDAL directly (through gdalwarp). You can call all gdal options quite easily in R with the gdalUtils package but you'll have to import the results back into R after hand.
EDITS following comment from OP:
Using the raster package:
library(raster)
Loading the tif:
rr <- raster("C:\\temp\\n0r_201601011100.tif")
Save you pixel coordinates equations in functions. Noticed I changed the Lat function (removed the negative sign, it didn't work with it, you'll have to validate that)
Lon = function(JJ) 0.01 * JJ + 162
Lat = function(II) 0.01 * II + 50.0
Get the extent of your raw raster in pixel coordinates:
ext.rr <- extent(rr)
Prepare a new empty raster which will be projected, have the good resolution and extent:
rr2 <- raster(nrows=nrow(rr), ncols=ncol(rr), xmn=Lon(ext.rr#xmin), xmx=Lon(ext.rr#xmax), ymn=Lat(ext.rr#ymin), ymx=Lat(ext.rr#ymax))
Fill this new raster with your modified values (following the equation you gave in the comments):
values(rr2) <- (values(rr) - 7) * 5
And you get:
rr2
class : RasterLayer
dimensions : 2600, 6000, 15600000 (nrow, ncol, ncell)
resolution : 0.01, 0.01 (x, y)
extent : 162, 222, 50, 76 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : layer
values : -35, 50 (min, max)
Notice that the lat-long projection was automatically pick-up by the raster function. Hopefully it's what you are looking for.

Constructing a bubble trellis plot with lattice in R

First off, this is a homework question. The problem is ex. 2.6 from pg.26 of An Introduction to Applied Multivariate Analysis. It's laid out as:
Construct a bubble plot of the earthquake data using latitude and longitude as the scatterplot and depth as the circles, with greater depths giving smaller circles. In addition, divide the magnitudes into three equal ranges and label the points in your bubble plot with a different symbol depending on the magnitude group into which the point falls.
I have figured out that symbols, which is in base graphics does not work well with lattice. Also, I haven't figured out if lattice has the functionality to change symbol size (i.e. bubble size). I bought the lattice book in a fit of desperation last night, and as I see in some of the examples, it is possible to symbol color and shape for each "cut" or panel. I am then working under the assumption that symbol size could then also be manipulated, but I haven't been able to figure out how.
My code looks like:
plot(xyplot(lat ~ long | cut(mag, 3), data=quakes,
layout=c(3,1), xlab="Longitude", ylab="Latitude",
panel = function(x,y){
grid.circle(x,y,r=sqrt(quakes$depth),draw=TRUE)
}
))
Where I attempt to use the grid package to draw the circles, but when this executes, I just get a blank plot. Could anyone please point me in the right direction? I would be very grateful!
Here is the some code for creating the plot that you need without using the lattice package. I obviously had to generate my own fake data so you can disregard all of that stuff and go straight to the plotting commands if you want.
####################################################################
#Pseudo Data
n = 20
latitude = sample(1:100,n)
longitude = sample(1:100,n)
depth = runif(n,0,.5)
magnitude = sample(1:100,n)
groups = rep(NA,n)
for(i in 1:n){
if(magnitude[i] <= 33){
groups[i] = 1
}else if (magnitude[i] > 33 & magnitude[i] <=66){
groups[i] = 2
}else{
groups[i] = 3
}
}
####################################################################
#The actual code for generating the plot
plot(latitude[groups==1],longitude[groups==1],col="blue",pch=19,ylim=c(0,100),xlim=c(0,100),
xlab="Latitude",ylab="Longitude")
points(latitude[groups==2],longitude[groups==2],col="red",pch=15)
points(latitude[groups==3],longitude[groups==3],col="green",pch=17)
points(latitude[groups==1],longitude[groups==1],col="blue",cex=1/depth[groups==1])
points(latitude[groups==2],longitude[groups==2],col="red",cex=1/depth[groups==2])
points(latitude[groups==3],longitude[groups==3],col="green",cex=1/depth[groups==3])
You just need to add default.units = "native" to grid.circle()
plot(xyplot(lat ~ long | cut(mag, 3), data=quakes,
layout=c(3,1), xlab="Longitude", ylab="Latitude",
panel = function(x,y){
grid.circle(x,y,r=sqrt(quakes$depth),draw=TRUE, default.units = "native")
}
))
Obviously you need to tinker with some of the settings to get what you want.
I have written a package called tactile that adds a function for producing bubbleplots using lattice.
tactile::bubbleplot(depth ~ lat*long | cut(mag, 3), data=quakes,
layout=c(3,1), xlab="Longitude", ylab="Latitude")