0

So I extracted the boundaries from DBSCAN in python and am now working on R to create visualization in GGPLOT and LEAFLET but I can't get the polygons to plot. This is a sample of the data:

[1,] "[(-98.898765, 19.455622), (-98.90185646, 19.44840652), (-98.91575986, 19.45534196), (-98.91887031, 19.46230492), (-98.91900371, 19.46275993), (-98.89589915, 19.46653405), (-98.89548853, 19.46623211), (-98.89525595, 19.46604594), (-98.897175, 19.45978824), (-98.898765, 19.455622)]"

[2,] "[(-98.84953814, 19.55776871), (-98.86278253, 19.55505466), (-98.86600973, 19.56443923), (-98.86597848, 19.56479789), (-98.86202303, 19.57997289), (-98.85983876, 19.58694556), (-98.84350132, 19.59316276), (-98.84325275, 19.59280415), (-98.83970199, 19.58690075), (-98.83834813, 19.58301972), (-98.84953814, 19.55776871)]"

I'm guessing I have to extract each number to create separate columns for latitude and longitude and be able to run the ggplot? I've tried to split it, extract it, and many more methods but getting various errors for example:

Error in UseMethod("extract_") : no applicable method for 'extract_' applied to an object of class "factor"

I've tried to do it on one cell only to get grips first but negative results always.

Any suggestions?

1

To plot your coordinate string on Leaflet in R:

Include the leaflet library:

install.packages('leaflet') library('leaflet')

Assign original string to a variable:

s1 <- "[(-98.898765, 19.455622), (-98.90185646, 19.44840652), (-98.91575986, 19.45534196), (-98.91887031, 19.46230492), (-98.91900371, 19.46275993), (-98.89589915, 19.46653405), (-98.89548853, 19.46623211), (-98.89525595, 19.46604594), (-98.897175, 19.45978824), (-98.898765, 19.455622)]"

Strip out unwanted characters:

c1 <- chartr('()[]',' ', s1)

Turn the cleaned string into a two column matix (Like @Spacedman said):

m1 <- matrix(as.numeric(strsplit(c1,",")[[1]]),ncol=2,byrow=TRUE)

make the Leaflet map and plot it:

map <- leaflet() map <- addTiles(map) map <- addPolygons(map, data = m1, color = "blue", weight = 4, smoothFactor = 0.5,opacity = 1.0, fillOpacity = 0.5,fillColor = 'red') map

You could also use SpatialPolygons like this past answer to a similar question.
  • Thank you Paul! this was a step ahead though.. what about plotting many polygons in the same map? – Mercedes Landa Jul 4 '17 at 10:17
1

Starting with a string like this:

> s
[1] "[(-98.898765, 19.455622), (-98.90185646, 19.44840652), (-98.91575986, 19.45534196), (-98.91887031, 19.46230492), (-98.91900371, 19.46275993), (-98.89589915, 19.46653405), (-98.89548853, 19.46623211), (-98.89525595, 19.46604594), (-98.897175, 19.45978824), (-98.898765, 19.455622)]"

If you replace all the different brackets with spaces using chartr, then you are left with a comma-separated string you can split with strsplit, and then convert to a 2-column matrix:

> matrix(as.numeric(strsplit(chartr('([)]','    ',s),",")[[1]]),ncol=2,byrow=TRUE)
           [,1]     [,2]
 [1,] -98.89876 19.45562
 [2,] -98.90186 19.44841
 [3,] -98.91576 19.45534
 [4,] -98.91887 19.46230
 [5,] -98.91900 19.46276
 [6,] -98.89590 19.46653
 [7,] -98.89549 19.46623
 [8,] -98.89526 19.46605
 [9,] -98.89718 19.45979
[10,] -98.89876 19.45562

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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