0

I'm searching for a way to plot some points for which I have the longitude and latitude (using Google Maps API) on a map created with centroids. I succeeded to do it by using functions such as map_data() and functions from the package ggplot2.

Results (just to show it really works) :

points on a map

However I don't like the french departments datas given by map_data(), it's quite ambiguous but the nice thing is that their coordinates are given by longitude and latitude. (french departments are similar to counties)

What I would like to do is to use a shapefile from a site such as GEOFLA (here) that provides me datas in projection LAMBERT-93 to plot my map. However the shapefile only contains the coordinates of deparments centroids so, since I have, in one hand, coordinates given by longitude and latitude and, in the other hand, coordinates given by centroids, so I can't plot the points on my map since the coordinates are not in the same "unit".

That's what I get with summary() on the shapefile :

Object of class SpatialPolygonsDataFrame
Coordinates:
        min     max
x   99217.1 1242417
y 6049646.3 7110480
Is projected: TRUE 
proj4string :
[+proj=lcc +lat_1=44 +lat_2=49 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +units=m
+no_defs]

Summary of map_data() (I show only the coordinates) :

      long               lat     
 Min.   :-5.14209   Min.   :41.37
 1st Qu.: 0.05213   1st Qu.:44.41
 Median : 2.45500   Median :46.79
 Mean   : 2.45851   Mean   :46.47
 3rd Qu.: 4.97283   3rd Qu.:48.44
 Max.   : 9.56267   Max.   :51.10

This shows that the coordinates used are different.

I tried many things such as using proj4string(), sptransform() for example to get the same units but I didn't get what I wanted.

So how should I do to get the same units for my two differents types of coordinates ? Is there a way to get the longitude and latitude from the centroids of the shapefile or should I use a function (if it existed) that can transform my points with longitude and latitude into centroids ?

This is my first time using R for maps and polygons, projections etc. also so don't be surprised if I said some real stupid things.

Exemple of points I want to plot :

  centerid latitude longitude                                       adresse_complete
1   0121H1 46.22368  5.210819               900 Route de Paris, 01440 Viriat, France
2   0121H2 46.22368  5.210819               900 Route de Paris, 01440 Viriat, France
3   0218H3 49.37008  3.335127 46 Avenue du Général de Gaulle, 02200 Soissons, France
4   0303H1 46.34292  2.608996       18 Avenue du 8 Mai 1945, 03100 Montluçon, France
5   0303H2 46.34292  2.608996       18 Avenue du 8 Mai 1945, 03100 Montluçon, France

dput() from above :

structure(list(centerid = c("0121H1", "0121H2", "0218H3", "0303H1", 
"0303H2"), latitude = c(46.2236804, 46.2236804, 49.3700842, 46.3429172, 
46.3429172), longitude = c(5.2108193, 5.2108193, 3.335127, 2.6089958, 
2.6089958), adresse_complete = c("900 Route de Paris, 01440 Viriat, France", 
"900 Route de Paris, 01440 Viriat, France", "46 Avenue du Général de Gaulle, 02200 Soissons, France", 
"18 Avenue du 8 Mai 1945, 03100 Montluçon, France", "18 Avenue du 8 Mai 1945, 03100 Montluçon, France"
)), .Names = c("centerid", "latitude", "longitude", "adresse_complete"
), row.names = c(NA, 5L), class = "data.frame")
5
  • In short, spTransform(shp, CRS("init=epsg:4326")) will give you a long-lat version of the data from your shapefile, and rgdal::project(as.matrix(longlat), proj4string(shp)) will convert the longlat values to the same projection as the shapefile data. Creating sp objects from scratch is pretty painful, but if you add more of the code you are using we can probably explain better. (It is confusing, sp and ggplot2 are essentially at-odds with each other, everything they do is different and there's no common language for how they work, though both are very sensible in their own way).
    – mdsumner
    Commented Apr 19, 2017 at 10:28
  • @mdsumner The only code I have is the code I used to get the map. Do you want to see it if it can help ? To be clear : the shapefile is plotting by using centroids and I have some points to plot but I have only their longitude and latitude (from Google Maps API) so I just need to find a way to express both of my datas in the same unit to be able to plot. ggplot2 was only used to get the first map, I'm not forced to use it if it's not necessary.
    – Mbr Mbr
    Commented Apr 19, 2017 at 10:41
  • @mdsumner It seems that your line : spTransform(shp, CRS("init=epsg:4326")) is working for one point (Paris) : points(2.349,48.853,pch=20,col="red",cex=2) so do you think I will be able to plot the other points using ggplot2 functions such as geom_point for example ? By the way, could you explain what is "init=epsg:4326" ? Anyway, thanks for the tip.
    – Mbr Mbr
    Commented Apr 19, 2017 at 11:27
  • CRS is the coordinates reference system. This is the projection of your dataset. You should take introduction courses about what is GIS and spatial data, because this is really the basis of GIS. spTransform can work for any spatial object, so that you should first transform your matrix of coordinates into SpatialPoints and then you'll be able to project it. Commented Apr 19, 2017 at 12:06
  • @StatnMap That's not what I was asking for, already understood what CRS is used for, I was asking about the value of CRS. Moreover I don't have a matrix with the shapefile I used, it's a S4 class object.
    – Mbr Mbr
    Commented Apr 19, 2017 at 12:27

1 Answer 1

1

You can either reproject your polygon shapefile OR your dataset. EPSG is a short code for spatial projections. EPSG:4326 is the code for non-projected data in degrees using wgs84: http://spatialreference.org/ref/epsg/4326/

Reproject your polygon into 4326

france_wgs84 <- spTransform(france_L93, CRS("+init=epsg:4326"))

Reproject your dataset into Lambert93

data <- structure(list(centerid = c("0121H1", "0121H2", "0218H3", "0303H1", 
"0303H2"), latitude = c(46.2236804, 46.2236804, 49.3700842, 46.3429172, 
46.3429172), longitude = c(5.2108193, 5.2108193, 3.335127, 2.6089958, 
2.6089958), adresse_complete = c("900 Route de Paris, 01440 Viriat, France", 
"900 Route de Paris, 01440 Viriat, France", "46 Avenue du Général de Gaulle, 02200 Soissons, France", 
"18 Avenue du 8 Mai 1945, 03100 Montluçon, France", "18 Avenue du 8 Mai 1945, 03100 Montluçon, France"
)), .Names = c("centerid", "latitude", "longitude", "adresse_complete"
), row.names = c(NA, 5L), class = "data.frame")


coordinates(data) <- ~longitude+latitude
proj4string(data) <- "+init=epsg:4326"
data_L93 <- spTransform(data, CRS("+proj=lcc +lat_1=44 +lat_2=49 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +units=m
+no_defs"))
1
  • Didn't know I should use to get first the non-projected data, that's why it never works when I tried. Thanks a lot.
    – Mbr Mbr
    Commented Apr 19, 2017 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.