Re: [R-sig-Geo] Adding colour to polylines in Leaflet

2018-09-24 Thread Dhiraj Khanna
@Kent Johnson  thank you so much for this. It does take
a bit of time to compute, but works nonetheless. Thanks again, appreciate!
Regards

Dhiraj Khanna
Mob:09873263331


On Sun, Sep 23, 2018 at 7:12 PM Kent Johnson  wrote:

> Another approach is to draw individual line segments instead of polylines.
> Here is one way; maybe there is a more elegant way to construct the
> segments but this works...
>
> library(tidyverse)
> library(sf)
> x = x %>% mutate(lat2=lead(lat, default=tail(lat, 1)),
>  lon2=lead(lon, default=tail(lon, 1))) %>%
>   mutate(segment = st_sfc(crs=4326,
>   pmap(.,
>  function(lat, lon, lat2, lon2, ...)
>st_linestring(matrix(c(lon, lat, lon2, lat2),
> byrow=TRUE, ncol=2)
>
> leaflet(x$segment) %>%
>   addTiles() %>%
>   addPolylines(color=x$Color)
>
> Kent
>
> On Sat, Sep 22, 2018 at 2:00 PM Kent Johnson  wrote:
>
>> Your problem is not really a leaflet problem, it is about identifying
>> runs in your data. This should help:
>> https://stackoverflow.com/q/43875716/80626
>>
>> Kent
>>
>> On Sat, Sep 22, 2018 at 12:22 PM Dhiraj Khanna 
>> wrote:
>>
>>> @Kent Johnson  guess I jumped the gun!
>>>
>>> Your code worked like a charm as long as the colours were all in order,
>>> ie, none of them are repeating.
>>> Like I mentioned, I am working with shipping data and the Color
>>> variable is dependent on the ship’s speed. The code that you provided joins
>>> all the line segments which have the same color.
>>> So if red represents a speed less than 3 knots, then it will join all
>>> the points irrespective of the timeline wherever the color is red.
>>>
>>> What I am looking for is one continuous path where the same color might
>>> repeat. Here’s a reproducible example:
>>>
>>> library(leaflet)
>>> x <- structure(list(lat = c(51.88783, 51.8878441, 51.887825, 51.88659,  
>>> 51.8866959, 51.8874931, 51.89359, 51.8941269, 51.8977051, 51.8994331,  
>>> 51.90773, 51.91324, 51.91604, 51.9216652, 51.93353, 51.9419365 ),
>>>  lon = c(4.28763342, 4.287635, 4.28765154, 4.29007339, 
>>> 4.29562664,  4.29917, 4.30641174, 4.30561829, 4.29263353, 4.284498, 
>>> 4.261132,  4.24711847, 4.241075, 4.23262, 4.21523666, 4.1927),
>>>  rateOfTurn = c(0L,  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
>>> 0L, 0L, 0L, 0L, 0L, 0L, 0L),
>>>  sogKts = c(0, 0, 0, 2.1, 3.4, 4.6, 3.5, 3.8, 7.4, 7.9, 
>>> 8.8,9.1, 9.2, 9.2, 0.3, 0.4),
>>>  cog = c(15, 15, 15, 122.2, 70.4,  70, 323.2, 
>>> 315.3, 289.3, 290.9, 303.8, 303.7, 308.9, 324.5, 304.9, 301.4),
>>>  heading = c(163, 162, 163, 106, 71, 71, 303,  298, 
>>> 289, 294, 303, 303, 310, 324, 304, 302),
>>>  timestamp = 
>>> c("2018-07-19T05:27:34","2018-07-19T05:39:35", "2018-07-19T05:45:34", 
>>> "2018-07-19T05:57:37",
>>>"2018-07-19T06:02:48", 
>>> "2018-07-19T06:04:49", "2018-07-19T06:12:51", "2018-07-19T06:13:32",
>>>"2018-07-19T06:19:08", 
>>> "2018-07-19T06:21:41",  "2018-07-19T06:28:42", "2018-07-19T06:32:50",
>>>"2018-07-19T06:34:37",  
>>> "2018-07-19T06:37:41", "2018-07-19T06:43:49", "2018-07-19T06:50:09"),
>>>  Color = c("red", "red", "red", "red", "orange", 
>>> "orange","orange", "orange", "orange", "orange", "yellow", "yellow",
>>>"yellow", "yellow", "red", "red")), 
>>> row.names = 32:47, class = "data.frame")
>>>
>>> #Kent's code
>>>
>>> x$lastColor = dplyr::lag(x$Color)
>>> map <-  leaflet(x)
>>> map <- addTiles(map)
>>> for( Color in
>>>  levels(as.factor(x$Color))){
>>>   map <- addPolylines(map,lng=~lon,lat=~lat,data=x[x$Color==Color | 
>>> x$lastColor==Color,], color=~Color) }
>>> map
>>>
>>> As you can see, the last two observations are again in red color. But
>>> when the map renders, it joins the last two

Re: [R-sig-Geo] Adding colour to polylines in Leaflet

2018-09-22 Thread Dhiraj Khanna
@Kent Johnson  guess I jumped the gun!

Your code worked like a charm as long as the colours were all in order, ie,
none of them are repeating.
Like I mentioned, I am working with shipping data and the Color variable is
dependent on the ship’s speed. The code that you provided joins all the
line segments which have the same color.
So if red represents a speed less than 3 knots, then it will join all the
points irrespective of the timeline wherever the color is red.

What I am looking for is one continuous path where the same color might
repeat. Here’s a reproducible example:

library(leaflet)
x <- structure(list(lat = c(51.88783, 51.8878441, 51.887825, 51.88659,
 51.8866959, 51.8874931, 51.89359, 51.8941269, 51.8977051, 51.8994331,
 51.90773, 51.91324, 51.91604, 51.9216652, 51.93353, 51.9419365 ),
 lon = c(4.28763342, 4.287635, 4.28765154,
4.29007339, 4.29562664,  4.29917, 4.30641174, 4.30561829, 4.29263353,
4.284498, 4.261132,  4.24711847, 4.241075, 4.23262, 4.21523666,
4.1927),
 rateOfTurn = c(0L,  0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
 sogKts = c(0, 0, 0, 2.1, 3.4, 4.6, 3.5, 3.8, 7.4,
7.9, 8.8,9.1, 9.2, 9.2, 0.3, 0.4),
 cog = c(15, 15, 15, 122.2, 70.4,  70, 323.2,
315.3, 289.3, 290.9, 303.8, 303.7, 308.9, 324.5, 304.9, 301.4),
 heading = c(163, 162, 163, 106, 71, 71, 303,
298, 289, 294, 303, 303, 310, 324, 304, 302),
 timestamp =
c("2018-07-19T05:27:34","2018-07-19T05:39:35", "2018-07-19T05:45:34",
"2018-07-19T05:57:37",
   "2018-07-19T06:02:48",
"2018-07-19T06:04:49", "2018-07-19T06:12:51", "2018-07-19T06:13:32",
   "2018-07-19T06:19:08",
"2018-07-19T06:21:41",  "2018-07-19T06:28:42",
"2018-07-19T06:32:50",
   "2018-07-19T06:34:37",
"2018-07-19T06:37:41", "2018-07-19T06:43:49", "2018-07-19T06:50:09"),
 Color = c("red", "red", "red", "red", "orange",
"orange","orange", "orange", "orange", "orange", "yellow", "yellow",
   "yellow", "yellow", "red", "red")),
row.names = 32:47, class = "data.frame")

#Kent's code

x$lastColor = dplyr::lag(x$Color)
map <-  leaflet(x)
map <- addTiles(map)
for( Color in
 levels(as.factor(x$Color))){
  map <- addPolylines(map,lng=~lon,lat=~lat,data=x[x$Color==Color |
x$lastColor==Color,], color=~Color) }
map

As you can see, the last two observations are again in red color. But when
the map renders, it joins the last two observations with the first three.

I am not sure what to do here? Inserting a row of NAs would help? But I am
also using another javascript plugin (polylineDecorator) for adding arrows
to the direction of travel and that is intolerant to NAs.
Appreciate some help here.


Regards
Dhiraj Khanna
Mob:09873263331

On Sat, Sep 1, 2018 at 8:06 PM Dhiraj Khanna  wrote:

> Thank you Kent, that worked like a charm!
> Regards
>
> Dhiraj Khanna
> Mob:09873263331
>
>
> On Sat, Sep 1, 2018 at 7:59 PM Kent Johnson  wrote:
>
>> You have to include the points where the colors change in both polylines.
>> Here is one way:
>>
>> x$lastColor = dplyr::lag(x$Color)
>> map <-  leaflet(x)
>> map <- addTiles(map)
>> for( Color in
>> levels(as.factor(x$Color))){
>>   map <- addPolylines(map,lng=~lon,lat=~lat,data=x[x$Color==Color |
>> x$lastColor==Color,], color=~Color) }
>> map
>>
>> Kent
>>
>> On Sat, Sep 1, 2018 at 8:56 AM, Dhiraj Khanna 
>> wrote:
>>
>>> @Kent they are appearing as three separate lines. I am hoping to see
>>> them joint with no gaps. The transition from row 4 to row 5 is where the
>>> speed has changed from 2.1 knots to 3.4 knots. I am hoping to see another
>>> line from row 4 to row 5 in red colour. Similarly for the other disjoint
>>> points.
>>> Regards
>>>
>>> Dhiraj Khanna
>>> Mob:09873263331
>>>
>>>
>>> On Sat, Sep 1, 2018 at 6:17 PM Kent Johnson  wrote:
>>>
>>>> Message: 5
>>>>> Date: Sat, 1 Sep 2018 08:28:24 +0530
>>>>> From: Dhiraj Khanna 
>>>>> To: r-sig-geo@r-project.org
>>>>> Subject: [R-sig-Geo] Adding colour to polylines in Leaflet
>>>>> Message-ID:
>>>>> <
>>>>> canhhk329-y7hpjd9gsos24msqgkrjc481oqspnagvisaw2j...@mail.gmail.com>
>>&

Re: [R-sig-Geo] Adding great circle routes as polylines in Leaflet

2018-09-03 Thread Dhiraj Khanna
Ben and Kent, thank you so much for your replies. Both work!

@Ben next time will make sure the code is more “calorie-free” :)

@Kent Johnson  I modified the weight for the lines by
using a scaling factor.

myScale <- function(x){(x-min(x))/(max(x)-min(x))}

ByRoute$AvgTCE <- myScale(ByRoute$AvgTCE)*5 + 1

@Kent Johnson  yes, this is shipping data and you are
right, great circle routes are not the best visualization. The problem I am
facing is that I have oil trade happening over a period of time from
various ports that I need to visualize. Over the selected period of time,
there are hundreds of voyages being undertaken by ships. Plotting them all
as gc routes looks ugly. My approach has been to classify these ports into
regions, which I drew using mapedit and saved them as SF polygons. I then
calculated their centroids and those are the coordinates in the ByRoute
dataframe. Would appreciate your comments on any other visualization which
you think might be appropriate.

Regards
Dhiraj Khanna
Mob:09873263331

On Mon, Sep 3, 2018 at 10:22 PM Kent Johnson  wrote:

> From: Dhiraj Khanna 
>> To: r-sig-geo@r-project.org
>> Subject: [R-sig-Geo] Adding great circle routes as polylines in
>> Leaflet
>> Message-ID:
>> > ix7z14j_fo...@mail.gmail.com>
>> Content-Type: text/plain; charset="utf-8"
>>
>> I am trying to add great circle routes between various regions in R.
>>
>> I would like to plot great circle routes between CommencingRegion to
>> LoadingRegion and then from LoadingRegion to DischargeRegion for every
>> row.
>> Additionally, every row needs to be in a different color and the thickness
>> needs to be proportional to AvgTCE. The last 6 variables in the data above
>> are the coordinates in Lat Long for the commencing, loading and
>> discharging
>> regions respectively. I am quite clueless on how to go about achieving
>> this. This is what I have tried and failed:
>>
>> leaflet() %>%
>>   addTiles() %>%
>>   for(i in 1:6){
>>
>> addPolylines(data=gcIntermediate(c(ByRoute$CLon[i],ByRoute$CLat[i]),c(ByRoute$LLon[i],ByRoute$CLat[i]),n=100,addStartEnd
>> = T,sp=T))
>>   }
>>
>> Here is a (rather clunky) start:
> library(leaflet)
> library(geosphere)
> library(dplyr)
>
> d = byRoute %>%
>   rowwise() %>%
>   do(leg1=gcIntermediate(c(.$CLon, .$CLat), c(.$LLon, .$LLat), n=50,
> addStartEnd=TRUE),
>  leg2=gcIntermediate(c(.$LLon, .$LLat), c(.$DLon, .$DLat), n=50,
> addStartEnd=TRUE))
>
> colors=palette()
> map = leaflet() %>% addTiles()
> for (i in seq_len(nrow(d)))
>   map = map %>% addPolylines(data=d$leg1[[i]], color=colors[i],
> weight=byRoute$AvgTCE[i]/3000-5) %>%
>   addPolylines(data=d$leg2[[i]], color=colors[i],
> weight=byRoute$AvgTCE[i]/3000-5)
> map
>
> You will have to split the great circles using something like
> the plot_my_connection function here:
>
> https://www.r-graph-gallery.com/how-to-draw-connecting-routes-on-map-with-r-and-great-circles/
>
> Is this shipping data? Maybe great circles are not the correct route...
> Kent
>
>
>> Regards
>> Dhiraj Khanna
>> Mob:09873263331
>
>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


[R-sig-Geo] Adding great circle routes as polylines in Leaflet

2018-09-02 Thread Dhiraj Khanna
I am trying to add great circle routes between various regions in R. Here’s
the sample data:

structure(list(CommencingRegion = c("RedSea", "EastAfrica", "GulfofMexico",
"FarEast", "RedSea", "GulfofMexico"), LoadRegion = c("RedSea",
"RedSea", "GulfofMexico", "FarEast", "RedSea", "GulfofMexico"
), DischargeRegion = c("NorthWestAfrica", "WestMedditerranean",
"WestCoastLatinAmerica", "AustraliaNewZealand", "WestMedditerranean",
"WestCoastCentralAmerica"), Count = c(1L, 1L, 2L, 1L, 2L, 5L),
AvgTCE = c(38879.53, 31783.55, 28520.79, 26068.8, 26054.28,
25883.81), CLon = c(37.8274879335485, 47.0791103099334, -90.9633701509553,
146.2727573458, 37.8274879335485, -90.9633701509553), CLat =
c(21.4460561443517,
-12.9570828789565, 25.2035802054683, 47.6530892619773, 21.4460561443517,
25.2035802054683), LLon = c(37.8274879335485, 37.8274879335485,
-90.9633701509553, 146.2727573458, 37.8274879335485, -90.9633701509553
), LLat = c(21.4460561443517, 21.4460561443517, 25.2035802054683,
47.6530892619773, 21.4460561443517, 25.2035802054683), DLon =
c(-17.1597117430475,
7.03639948506481, -73.4238157230412, 151.051220297802, 7.03639948506481,
255.83509305644), DLat = c(24.2308740597312, 38.8907379374372,
-25.8934046406896, -25.1880219406131, 38.8907379374372, 21.8130318388702
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

I would like to plot great circle routes between CommencingRegion to
LoadingRegion and then from LoadingRegion to DischargeRegion for every row.
Additionally, every row needs to be in a different color and the thickness
needs to be proportional to AvgTCE. The last 6 variables in the data above
are the coordinates in Lat Long for the commencing, loading and discharging
regions respectively. I am quite clueless on how to go about achieving
this. This is what I have tried and failed:

leaflet() %>%
  addTiles() %>%
  for(i in 1:6){

addPolylines(data=gcIntermediate(c(ByRoute$CLon[i],ByRoute$CLat[i]),c(ByRoute$LLon[i],ByRoute$CLat[i]),n=100,addStartEnd
= T,sp=T))
  }

Regards
Dhiraj Khanna
Mob:09873263331

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Adding colour to polylines in Leaflet

2018-09-01 Thread Dhiraj Khanna
Thank you Kent, that worked like a charm!
Regards

Dhiraj Khanna
Mob:09873263331


On Sat, Sep 1, 2018 at 7:59 PM Kent Johnson  wrote:

> You have to include the points where the colors change in both polylines.
> Here is one way:
>
> x$lastColor = dplyr::lag(x$Color)
> map <-  leaflet(x)
> map <- addTiles(map)
> for( Color in
> levels(as.factor(x$Color))){
>   map <- addPolylines(map,lng=~lon,lat=~lat,data=x[x$Color==Color |
> x$lastColor==Color,], color=~Color) }
> map
>
> Kent
>
> On Sat, Sep 1, 2018 at 8:56 AM, Dhiraj Khanna 
> wrote:
>
>> @Kent they are appearing as three separate lines. I am hoping to see them
>> joint with no gaps. The transition from row 4 to row 5 is where the speed
>> has changed from 2.1 knots to 3.4 knots. I am hoping to see another line
>> from row 4 to row 5 in red colour. Similarly for the other disjoint points.
>> Regards
>>
>> Dhiraj Khanna
>> Mob:09873263331
>>
>>
>> On Sat, Sep 1, 2018 at 6:17 PM Kent Johnson  wrote:
>>
>>> Message: 5
>>>> Date: Sat, 1 Sep 2018 08:28:24 +0530
>>>> From: Dhiraj Khanna 
>>>> To: r-sig-geo@r-project.org
>>>> Subject: [R-sig-Geo] Adding colour to polylines in Leaflet
>>>> Message-ID:
>>>> <
>>>> canhhk329-y7hpjd9gsos24msqgkrjc481oqspnagvisaw2j...@mail.gmail.com>
>>>> Content-Type: text/plain; charset="utf-8"
>>>>
>>>> I am working with shipping data where I get the dynamic parameters of a
>>>> ship like its position, speed, heading and rate of turn. I am then
>>>> trying
>>>> to plot this on a leaflet map and trying to colour the polylines based
>>>> on
>>>> the speed, but it always shows up in the same colour. Here’s some sample
>>>> data:
>>>>
>>>> 
>>>> This is the code I have tried which doesn’t work:-
>>>>
>>>> map <-  leaflet(x) map <- addTiles(map) for( Color in
>>>> levels(as.factor(x$Color))){   map <- addPolylines(map,
>>>> lng=~lon,lat=~lat,data=x[x$Color==Color,], color=~Color) } map
>>>>
>>>> Regards
>>>> Dhiraj Khanna
>>>> Mob:09873263331
>>>
>>>
>>> What are you expecting to see? When I run your code I get a map with
>>> three lines, one red, one orange and one yellow.
>>>
>>> Kent
>>>
>>>
>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Adding colour to polylines in Leaflet

2018-09-01 Thread Dhiraj Khanna
@Kent they are appearing as three separate lines. I am hoping to see them
joint with no gaps. The transition from row 4 to row 5 is where the speed
has changed from 2.1 knots to 3.4 knots. I am hoping to see another line
from row 4 to row 5 in red colour. Similarly for the other disjoint points.
Regards

Dhiraj Khanna
Mob:09873263331


On Sat, Sep 1, 2018 at 6:17 PM Kent Johnson  wrote:

> Message: 5
>> Date: Sat, 1 Sep 2018 08:28:24 +0530
>> From: Dhiraj Khanna 
>> To: r-sig-geo@r-project.org
>> Subject: [R-sig-Geo] Adding colour to polylines in Leaflet
>> Message-ID:
>> <
>> canhhk329-y7hpjd9gsos24msqgkrjc481oqspnagvisaw2j...@mail.gmail.com>
>> Content-Type: text/plain; charset="utf-8"
>>
>> I am working with shipping data where I get the dynamic parameters of a
>> ship like its position, speed, heading and rate of turn. I am then trying
>> to plot this on a leaflet map and trying to colour the polylines based on
>> the speed, but it always shows up in the same colour. Here’s some sample
>> data:
>>
>> 
>> This is the code I have tried which doesn’t work:-
>>
>> map <-  leaflet(x) map <- addTiles(map) for( Color in
>> levels(as.factor(x$Color))){   map <- addPolylines(map,
>> lng=~lon,lat=~lat,data=x[x$Color==Color,], color=~Color) } map
>>
>> Regards
>> Dhiraj Khanna
>> Mob:09873263331
>
>
> What are you expecting to see? When I run your code I get a map with three
> lines, one red, one orange and one yellow.
>
> Kent
>
>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


[R-sig-Geo] Adding colour to polylines in Leaflet

2018-08-31 Thread Dhiraj Khanna
I am working with shipping data where I get the dynamic parameters of a
ship like its position, speed, heading and rate of turn. I am then trying
to plot this on a leaflet map and trying to colour the polylines based on
the speed, but it always shows up in the same colour. Here’s some sample
data:


structure(list(lat = c(51.88783, 51.8878441, 51.887825, 51.88659,
51.8866959, 51.8874931, 51.89359, 51.8941269, 51.8977051, 51.8994331,
51.90773, 51.91324, 51.91604, 51.9216652, 51.93353, 51.9419365 ), lon
= c(4.28763342, 4.287635, 4.28765154, 4.29007339, 4.29562664,
4.29917, 4.30641174, 4.30561829, 4.29263353, 4.284498, 4.261132,
4.24711847, 4.241075, 4.23262, 4.21523666, 4.1927), rateOfTurn = c(0L,
 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
sogKts = c(0, 0, 0, 2.1, 3.4, 4.6, 3.5, 3.8, 7.4, 7.9, 8.8,  9.1,
9.2, 9.2, 9.3, 9.3), cog = c(15, 15, 15, 122.2, 70.4,  70, 323.2,
315.3, 289.3, 290.9, 303.8, 303.7, 308.9, 324.5,  304.9, 301.4),
heading = c(163, 162, 163, 106, 71, 71, 303,  298, 289, 294, 303,
303, 310, 324, 304, 302), timestamp = c("2018-07-19T05:27:34",
"2018-07-19T05:39:35", "2018-07-19T05:45:34", "2018-07-19T05:57:37",
   "2018-07-19T06:02:48", "2018-07-19T06:04:49",
"2018-07-19T06:12:51",  "2018-07-19T06:13:32",
"2018-07-19T06:19:08", "2018-07-19T06:21:41",
"2018-07-19T06:28:42", "2018-07-19T06:32:50", "2018-07-19T06:34:37",
   "2018-07-19T06:37:41", "2018-07-19T06:43:49", "2018-07-19T06:50:09"
), Color = c("red", "red", "red", "red", "orange", "orange",
"orange", "orange", "orange", "orange", "yellow", "yellow",
"yellow", "yellow", "yellow", "yellow")), row.names = 32:47, class =
"data.frame")

This is the code I have tried which doesn’t work:-

map <-  leaflet(x) map <- addTiles(map) for( Color in
levels(as.factor(x$Color))){   map <- addPolylines(map,
lng=~lon,lat=~lat,data=x[x$Color==Color,], color=~Color) } map

Regards
Dhiraj Khanna
Mob:09873263331

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Help on pointLabel() function for engineering drawings

2015-09-28 Thread Dhiraj Khanna
Oscar, I did as you suggested by putting “” for labels that I didn’t want.
However, the labels still overlap the drawing :( Here is my code:

library(dplyr)library(maptools)#Known Size of Sheet
xmin <- -357.7
xmax <- 19.3
ymin <- -90.2
ymax <- 159.7
#Read in the data
linescoords <- read.csv("Linecoords.csv")
#Coordinates translation
linescoords$X  <- linescoords$X - xmin
linescoords$Y <- linescoords$Y - ymin
# Plot the figure
xlimit <- c(-10, abs(xmin-xmax)+10 )
ylimit <- c(-10, abs(ymin-ymax)+10 )
plot(linescoords$X, linescoords$Y, "p", xlim=xlimit, ylim = ylimit)

i <- seq(1, nrow(linescoords), 2)for(x in i){
  segments(linescoords$X[x], linescoords$Y[x],
   linescoords$X[x+1], linescoords$Y[x+1])
}
#Read in text labels data
labels <- read.csv("labels.csv")
#Text labels Coordinates translation
labels$x <- labels$x - xmin
labels$y <- labels$y - ymin
labels$xminl <- labels$xminl - xmin
labels$xmaxl <- labels$xmaxl - xmin
labels$yminl <- labels$yminl - ymin
labels$ymaxl <- labels$ymaxl - ymin
#Point of Origin for labels
points(labels$x, labels$y, pch=16, col="red")
#Width & Height of labels
labels <- labels %>% mutate(width=abs(xminl-xmaxl), height=abs(yminl-ymaxl))

oddseq <- seq(1, nrow(linescoords),2)
evenseq <- seq(2, nrow(linescoords),2)

x1 <- linescoords$X[oddseq]
y1 <- linescoords$Y[oddseq]
x2 <- linescoords$X[evenseq]
y2 <- linescoords$Y[evenseq]
lineseg <- seq(1,length(x1))

lineslope <- as.data.frame(cbind(lineseg, x1, y1, x2, y2))

lineslope <- mutate(lineslope, slope=(y2-y1)/(x2-x1))

pointsx <- vector("list")
pointsy <- vector("list")for (s in 1:nrow(lineslope)){

  if(lineslope[s,2]==lineslope[s,4]){
  louty <- abs(lineslope[s,3]-lineslope[s,5])/3
  yp <- seq(lineslope[s,3], lineslope[s,5], length.out=louty)
  pointsy[[s]] <- yp
  xp <- seq(lineslope[s,2], lineslope[s,4], length.out=louty)
  pointsx[[s]] <- xp
  }
  else{
loutx <- abs(lineslope[s,2]-lineslope[s,4])/3
xp <- seq(lineslope[s,2], lineslope[s,4], length.out=loutx)
pointsx[[s]] <- xp
yp <- seq(lineslope[s,3], lineslope[s,5], length.out=loutx)
pointsy[[s]] <- yp
  }
}

finalx <- unlist(pointsx)
finaly <- unlist(pointsy)

extralabels <- vector("character", length=length(finalx))
labelsfinal <- append(as.character(labels$Point), extralabels)
X <- append(labels$x, finalx)
Y <- append(labels$y, finaly)
plot(finalx, finaly, "p")
pointLabel(X, Y, labelsfinal)
points(labels$x, labels$y, pch=16, col="red")

​

Regards

Dhiraj Khanna
Mob:09873263331

On Sun, Sep 27, 2015 at 9:58 PM, Oscar Perpiñán Lamigueiro <
oscar.perpi...@upm.es> wrote:

> > Will using the lineLabel function align the labels as per the
> > orientation of the line?
>
> Yes, exactly.
>
> > Also, I changed the lines to points and then tried the pointLabel
> > function.  However, it ignores all the points that are not mentioned
> > in the argument of the function and still plots over them. Any
> > workaround for this?
>
> You may include empty characters ("") as elements in the _labels_
> vector. For example:
>
> x <- 1:5
> y <- 1:5
> labels <- c('A', 'B', '', 'D', '')
> plot(x, y)
> pointLabel(x, y, labels)
>
> Best.
>
> Oscar.
>
> --
> Oscar Perpiñán Lamigueiro
> Dpto. Ing. Eléctrica, Electrónica, Automática y Física Aplicada
> Escuela Técnica Superior de Ingeniería y Diseño Industrial
> URL: http://oscarperpinan.github.io
>
___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


[R-sig-Geo] Help on pointLabel() function for engineering drawings

2015-09-24 Thread Dhiraj Khanna
Hello,

<http://stackoverflow.com/questions/32717742/automatic-label-plotting-using-pointlabel-from-maptools-library-in-r#>

I am trying to solve automatic label placement problem for an engineering
drawing using the Simulated Annealing algorithm. I came across the
pointLabel() function in the maptools library in R. However, what I found
was that while the function is able to optimally place the labels, it
doesn't take into account the underlying drawing. I don't want the labels
to overlap with the drawing lines. Is there some way I can achieve this?
Also, my labels are of varying size in terms of width and height. Is there
any way in which I can specify the dimension of each label apart from the
point of origin of the labels?

This is my code and a link to the requisite csv files:
https://www.dropbox.com/s/85qqs4nlvm4crck/TextBoxData.rar?dl=0

library(dplyr)
library(maptools)
#Known Size of Sheet
xmin <- -357.7
xmax <- 19.3
ymin <- -90.2
ymax <- 159.7
#Read in the data
linescoords <- read.csv("Linecoords.csv")
#Coordinates translation
linescoords$X  <- linescoords$X - xmin
linescoords$Y <- linescoords$Y - ymin
#Plot the figure
xlimit <- c(-10, abs(xmin-xmax)+10 )
ylimit <- c(-10, abs(ymin-ymax)+10 )
plot(linescoords$X, linescoords$Y, "p", xlim=xlimit, ylim = ylimit)

i <- seq(1, nrow(linescoords), 2)for(x in i){
segments(linescoords$X[x], linescoords$Y[x],
 linescoords$X[x+1], linescoords$Y[x+1])}

#Read in text labels data
labels <- read.csv("labels.csv")
#Text labels Coordinates translation
labels$x <- labels$x - xmin
labels$y <- labels$y - ymin
labels$xminl <- labels$xminl - xmin
labels$xmaxl <- labels$xmaxl - xmin
labels$yminl <- labels$yminl - ymin
labels$ymaxl <- labels$ymaxl - ymin
#Point of Origin for labels
points(labels$x, labels$y, pch=16, col="red")
#Width & Height of labels
labels <- labels %>% mutate(width=abs(xminl-xmaxl), height=abs(yminl-ymaxl))

pointLabel(labels$x, labels$y, labels$Point)

Regards

Dhiraj Khanna
Mob:09873263331

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo