I received a .gdb file that I need to do analysis on. This is an ESRI file, but I don't have access to any ESRI products. I opened it in R using the "rgdal" package, but the coordinates it displays seem very off.
For example, (-9288065, 4604652) should map to Kentucky. My initial thought was that there are missing decimals, so it should be -92.88 and -46.04, but this maps to somewhere north of Kentucky, which is wrong.
Do these coordinate units look familiar? How might I convert them to latitude and longitude?
Solved with help from #camille.
For future users:
library(rgdal)
library(raster)
myFeatureClass<-readOGR('your_data_here')
#checks the projection type
sp::proj4string(myFeatureClass)
#convert projection to longlat
final_data <- as.data.frame(spTransform(myFeatureClass, CRS("+proj=longlat +datum=WGS84")))
Related
I am a new user of R and I have a project where some coordinates are provided in British National Grid coordinates. These are not recognised by our preferred mapping program, so I would prefer to convert to lat/long. I have found some resources on how to do this using the code below:
require("rgdal") || install.packages("rgdal")
require("sp") || install.packages("sp")
library(rgdal)
# prepare UTM coordinates matrix
utmcoor<-SpatialPoints(cbind(knime.in$"Easting",knime.in$"Northing"), proj4string=CRS("+proj=utm +zone=30"))
#utmdata$X and utmdata$Y are corresponding to UTM Easting and Northing, respectively.
#zone= UTM zone
# converting
longlatcoor<-spTransform(utmcoor,CRS("+proj=longlat"))
However, the results are not what I was expecting. I tried an Easting/Northing for somewhere in Greenwich as an example and the point eventually mapped was somewhere off the Ivory Coast! I don't think UTM here is the correct system to specify, but I do not know how to specify BNG. I don't think I'm too far off solving the problem, but I could do with a bit of assistance. Thank you!
Example:
Tried the above code with East./North. 537869 , 178976. Was expecting 51.492927 , -0.015449524, but got the result -2.6595478348955846, 1.6192189249637292 instead.
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")
I have a shapefile that I imported into R using readOGR from the rgdal package. I do a little bit of work with it, like adding attribute information, etc, then export it as an ESRI shapefile again, with a new name. However, when I bring both the original and new shapefile into ArcGIS, it tells me that the CRS does not match.
So, noting that all the projection parameters remain the same, but the projection and coordinate system names are different, and the datum
name is dropped, my questionas are:
Is the second CRS the same as the first?
If so, why did the names change, and why does ArcGIS no longer recognize it as the same?
If not, how did it get changed?
Can the proj4string be modified to be more specific, and if so, why did readOGR not already do this to preserve all the information?
I can use the new shapefiles just fine, but it would be nice to know that
the CRS is identical to the original. And, I could of course define it again in ArcGIS, but part of the motivation to work in R
is to obviate the need to point and click for many files.
I appreciate any insights or enlightenment.
Here is the original projection information from ArcGIS:
Projected Coordinate System: NAD_1983_HARN_Transverse_Mercator
Projection: Transverse_Mercator
False_Easting: 520000.00000000
False_Northing: -4480000.00000000
Central_Meridian: -90.00000000
Scale_Factor: 0.99960000
Latitude_Of_Origin: 0.00000000
Linear Unit: Meter
Geographic Coordinate System: GCS_North_American_1983_HARN
Datum: D_North_American_1983_HARN
Prime Meridian: Greenwich
Angular Unit: Degree
Here is the proj4string from R, which also agrees with the proj4string given for this projection at www.spatialreference.org for epsg:3071 and also for SR-ORG:7396.
+proj=tmerc +lat_0=0 +lon_0=-90 +k=0.9996 +x_0=520000 +y_0=-4480000 +ellps=GRS80 +units=m +no_defs
When I use writeOGR to export the SpatialPolygonsDataFrame with the above proj4string, then bring it back into ArcGIS, the
projection information is given as the following, and is no longer recognized as the original.
Projected Coordinate System: Transverse_Mercator
Projection: Transverse_Mercator
false_easting: 520000.00000000
false_northing: -4480000.00000000
central_meridian: -90.00000000
scale_factor: 0.99960000
latitude_of_origin: 0.00000000
Linear Unit: Meter
Geographic Coordinate System: GCS_GRS 1980(IUGG, 1980)
Datum: D_unknown
Prime Meridian: Greenwich
Angular Unit: Degree
Perhaps not a definitive answer, but I posted this question on the R-sig-Geo list serve, and got a few possible work-around solutions. For now, I simply used an R-script to overwrite the .prj file with a copy of the original, and that seems to work fine. Also suggested was the use of a package called arcgisbinding to bridge ArcGIS and R (and maybe a similar solution would be available for QGIS?). I have not verified the arcgisbinding solution, but more information can be found at the the blog post here and in the package documentation here.
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
I am just getting started with geospatial data and have a question related to longitude/latitude. I downloaded the shapefile for the French departments (http://professionnels.ign.fr/geofla) and used R to read in the data:
library(rgdal)
library(maptools)
map <- readOGR(dsn="...",layer="DEPARTEMENT")
map_f <- fortify(map)
head(map_f)
If I look at the longitude-latitude, it are numbers like long = 885661.8, lat = 6679942. In contrast, the long-lat combination for Paris is 2.352222, 48.85661 (geocode("Paris")). So I don't really get why the numbers are so different in magnitude? Can somebody help me or give some good references?
Thanks in advance!
The coordinates look projected (perhaps UTM) and not lat,long. I think that the fields are just mislabeled. I do not read French but in quick scan of the site you linked this caught my eye:
En métropole : (RGF 93) projection Lambert93
En outre-mer : (système légal)- Projections UTM
Neither of which are geographic coordinate systems. Please track down the metadata and check the coordinate system. You can also check proj4string(map) to see it there is a coordinate system defined.