# [R-sig-Geo] Distances from points to coast do not make sense.

Agus Camacho agus.camacho at gmail.com
Wed Sep 17 23:13:58 CEST 2014

```Thanks again Robert,

In case it is of help for any body, after trying several combinations, the
fastest solution I got to calculate minimum distances to coast was:

require(PBSmapping)

wcoastline <- importShapefile("GSHHS_c_L1.shp")
w=as.matrix(wcoastline[,c('X','Y')])

# You will need x1, an object with: 1) long 2)lat coordinates

require(sp)
distance=rep(NA,dim(x1)[1])
for(n in 1:dim(x1)[1])
{
distance[n]=min(spDistsN1(w,cbind(x[n,1], x1[n,2]),longlat=TRUE))
}
meandist=mean(distance)#
}

Cheers,
Agus

2014-09-12 2:17 GMT-03:00 Robert J. Hijmans <r.hijmans at gmail.com>:

> On Thu, Sep 11, 2014 at 9:53 PM, Agus Camacho <agus.camacho at gmail.com>
> wrote:
> > Many thanks Robert, it actually works,
> >
> > I still would like using the coast lines from the file GSHHS_c_L1.shp I
> was
> > originally using because seems to be much faster and contain coast lines
> of
> > the world. I think it should work, however, i dont know why i cant take
> > correct distances from this lines.
> >
>
> should work too
>
> > Here is the script that im using for obtaining the world coastline from
> the
> > file and getting the distances, anybody might want the .shp file just
> > me.
> >
> > library(geosphere)
> > wcoastline = PBSmapping::importShapefile("GSHHS_c_L1.shp")
> > wcl = as.matrix(wcoastline[,c("X","Y")])
> >
> > dist2Line(cbind(141.3311, -6.826087), cl)[1]/1000# fast but wrong
> distance
>
> what is "cl"? Is it "wcl" but that is a matrix, not lines.
>
>
> >
> >
>
> library(maptools)
> library(raster)
> library(geosphere)
> w = shapefile("GSHHS_c_L1.shp")
> dist2Line(cbind(141.3311, -6.826087), w)[1]/1000# very slow but right
>
>
> Robert
>
> > Cheers and a hug for the people of the last Geostat!
> > Agus
> >
> >
> > 2014-09-11 21:25 GMT-03:00 Robert J. Hijmans <r.hijmans at gmail.com>:
> >
> >> The below seems to work correctly (albeit slowly).
> >>
> >>
> >> library(maptools)
> >> library(raster)
> >> library(geosphere)
> >>
> >> data(wrld_simpl)
> >> w <- aggregate(wrld_simpl)
> >> af = cbind(16, 13)
> >> ng <- cbind(139, -4)
> >> p <- rbind(af, ng)
> >> dist2Line(p, w)
> >>
> >>
> >> On Thu, Sep 11, 2014 at 4:13 PM, Agus Camacho <agus.camacho at gmail.com>
> >> wrote:
> >> > I have two species, one is widespread in Africa (varanus niloticus)
> and
> >> > the
> >> > other is distributed across a region of new Guinea (Varanus
> prasinus). I
> >> > calculated the mean distance to coast of their respective geographic
> >> > records using the following script:
> >> >
> >> > x1 is a dataframe with two columns: Longitude and Latitude of
> geographic
> >> > records
> >> >
> >> > meandist=rep(0,length(levels(x\$Species)))
> >> > distance=rep(NA,dim(x1)[1])
> >> >
> >> > for(n in 1:dim(x1)[1])
> >> > {
> >> >
> >> >
> distance[n]=(dist2Line(cbind(x1[n,1],x1[n,2]),LAcl,distfun=distVincentyEllipsoid)/1000)[1]
> >> > }
> >> > meandist=mean(distance)
> >> >
> >> > The problem is that geographic records from the african species is
> >> > giving
> >> > me shorter distances to the coast (mean=6677) than the species from
> new
> >> > guinea(mean=12417). I already plotted the species records and checked
> >> > there
> >> > are not bizarre records. I can send the records for anyone
> insterested.
> >> >
> >> > Thanks in advance.
> >> > Agus
> >> >
> >> >
> >> >
> >> > --
> >> > Agustín Camacho Guerrero.
> >> > Doutor em Zoologia.
> >> > Laboratório de Herpetologia, Departamento de Zoologia, Instituto de
> >> > Biociências, USP.
> >> > Rua do Matão, trav. 14, nº 321, Cidade Universitária,
> >> > São Paulo - SP, CEP: 05508-090, Brasil.
> >> >
> >> >         [[alternative HTML version deleted]]
> >> >
> >> >
> >> > _______________________________________________
> >> > R-sig-Geo mailing list
> >> > R-sig-Geo at r-project.org
> >> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >> >
> >
> >
> >
> >
> > --
> > Agustín Camacho Guerrero.
> > Doutor em Zoologia.
> > Laboratório de Herpetologia, Departamento de Zoologia, Instituto de
> > Biociências, USP.
> > Rua do Matão, trav. 14, nº 321, Cidade Universitária,
> > São Paulo - SP, CEP: 05508-090, Brasil.
>

--
Agustín Camacho Guerrero.
Doutor em Zoologia.
Laboratório de Herpetologia, Departamento de Zoologia, Instituto de
Biociências, USP.
Rua do Matão, trav. 14, nº 321, Cidade Universitária,
São Paulo - SP, CEP: 05508-090, Brasil.

[[alternative HTML version deleted]]

```