R Code: Converting lat and long in feet to degrees - r

I am new to the mapping functions in R. I am trying to plot points onto a map, however, I need them in degrees (they are currently in feet). I'm trying to figure out the code for transforming coordinates that are in feet to degrees, would anyone know the code or formula for this?

Not sure what you mean by this exactly, but in general to transform the projection of a spatial object you can use:
data.transformed <- spTransform(data, CRS("+proj=longlat +datum=WGS84"))
spTransform() is from the rgdal package, and the CRS() argument can be replaced with whichever particular projection you need. If this isn't helpful or if you are new to mapping and the concept of projections in general, I would suggest trying to ask a question on https://gis.stackexchange.com/.

Related

Creating a ternary plot from Cartesian coordinates

I am trying something - I want to estimate contributions from sources via a mixing triangle.
I have all data points in the Cartesian system (2 tracers measured)
Now I need to transform my coordinates into trilinear coordinates.
I was looking for a pre-existing function for that or another way to compute it on my own.
Can someone help me? Is there something existing already in R?
ggtern needs those trilinear coordinates if I did not misunderstood the manual.
Thanks,
Nadine

How can I edit the values of coordinates within a geojson file?

I am trying to map a geojson file (A map of Alaska precincts, downloaded from the division of elections and then converted online to geojson) onto a choropleth map using folium, the problem is the coordinates are in 7-digit numbers like this:
[ -16624764.227, 8465801.1497 ]
I read on a similar post that this was most likely a US coordinate system like UTM or State Plane, and recommended using an API to reproject it. Is it also possible to access the coordinates directly such as with geopandas and divide them by 100000?
The data is most likely in a specific cartographic projection. You don't just want to divide by 100k - the data will likely have nonlinear transformations which have a different effect on the position depending on the location. See the GeoPandas docs on working with projections.
If the CRS of the data is correctly encoded, you can re-project the dataframe into lat/lons (e.g. WGS84, which has the EPSG Code 4326) using geopandas.GeoDataFrame.to_crs, e.g.:
df_latlon = df.to_crs("epsg:4326")

Introducing random noise into point positions

I am using R and Leaflet for R to plot 1000s of points. The raw data is imported to a data frame from SQL Server as British National Grid (BNG) coordinates (Transverse Mercator) which are then converted to lat/long using rgdal before outputting to a stand-alone html via from a Leaflet widget.
The BNG coordinates have been produced by geocoding postcodes and so, when >1 person has the same postcode the coordinate is the same.
I would like to add some random noise to the last 2-digits of each easting/northing coordinate so that all points are likely to be visible in Leaflet. What would be the simplest way to achieve this?
thanks
mike
Two solutions to overplotting are
"jittering" the points by adding some random noise to their coordinates and
adding transparency to the point color so you can see point density.
x=jitter(x), y=jitter(y) will accomplish #1.
col=scales::alpha("blue", 0.5) will accomplish #2.

Create buffer around spatial data in R

I have a spatial dataset of shopping centers that I would like to create buffers around in R.
I think these packages will be useful:
require(maptools)
require(geosphere)
I was able to do so for a set of coordinates, but not for spatial data. The code looks like this:
coordinates(locs) <- c("Longitude", "Latitude") # set spatial coordinates
fivekm <- cbind(coordinates(locs), X=rowSums(distm (coordinates(locs)[,1:2], fun = distHaversine) / 1000 <= 5)) # number of points within 5 km
But I don't know what function/package to use for a set of polygons. Can someone please advise on the function (or code) and I will go from there?
Thanks!
In library rgeos, there is the gBuffer function that works with SpatialPoints or SpatialPolygons.
The width parameter allows to set the distance to which you want to buffer. However, be careful, this distance is in the scale of the coordinates system used. Thus, in degrees and not in meters with non-projected data. As suggested by #Ege Rubak, you will have to project your data with spTransform first (be sure to use the appropriate CRS according to your location).
As for now, rgeos library works with library sp, but not (yet?) with the recent sf.
I think the only option at the moment is to project your longitude and latitude points to a flat map and then do everything there. As far as I know there are no packages for doing polygonal geometry on the sphere yet (I'm working on one, but there is no ETA).
Projection used to be done with spTransform from the sp package, but now it may be more convenient to use the more modern simple features package sf which has the function st_transform. The vignette https://cran.r-project.org/web/packages/sf/vignettes/sf1.html has a section called "Coordinate reference systems and transformations" to help you with this part. The buffering is described in the section "Geometrical operations".
The two previous post have covered the details but I thought it might be helpful to provide a workflow. This is assuming you have you are using points of lat and long. What is your original spatial data format?
Convert your coordinates into a Spatial Points Dataframe SpatialPointsDataFrame and assign it a geographic CRS (proj4) that matches your coordinate data (probably WGS84)
Change the projection to a local projected CRS with preferred units
Apply buffer to spatial point data frame, the width will now be in more usable units

Create polygon on a map in right projection using R

In my project I am using different projections for my data. I want to learn what happens when you create shapes in a equivalent (equal area) projection and transform them to a conformal projection.
I have a SPDF in an equal area projection. I am trying to create some shapes in this SPDF to later transform them. I did not expect that this is a difficult task. I would like to work with vectors.
The main problem I think I have is that I am unaware of the package and function I should use. I have searched for a couple of hours this morning but can not find the right package or function. Also I can not find people asking questions about this exact topic, which surprises me.
So the question is: How do I make a shape in a SPDF using R?

Resources