Select to view content in your preferred language

arcgisbinding package in R

3116
4
Jump to solution
05-31-2017 01:51 AM
AnnieO_Donnell
Deactivated User

When using the `arcgisbinding` package in R, how do you access the x,y co-ordinate points of class `arc.shape` and type Polyline? There is documentation online here that explains how to do this for type Point, but the format for Polyline is different, it looks like a list of vectors as opposed to a single vector.

0 Kudos
1 Solution

Accepted Solutions
ShaunWalbridge
Esri Regular Contributor

Hello Annie O'Donnell‌,

The trick posted on the GitHub issue works nicely because the dimensionality of a point can easily be represented in a tabular form, by expanding the X and Y coordinates into two columns. For higher dimension objects like polyline, it doesn't work directly because of the dimensionality difference. While you could build up lists to represent this data, I think it'll be less work to convert the data into sp class objects. For Polyline, you'll want SpatialLinesDataFrame (sp documentation), or to manually populate a SpatialLines object from a collection of lists. Here's a short example updating the first coordinate of the first polyline, and saving it back to a new FGDB feature class:

library(arcgisbinding)
arc.check_product()

setwd('c:/temp')
input <- file.path(getwd(), 'r_test.gdb', 'polyline_example')

d <- arc.open(input)
df <- arc.select(d)
g <- arc.shape(df)
sp.df <- arc.data2sp(df)

first_polyline <- sp.df@lines[[1]]@Lines[[1]]@coords
# update the first Y coordinate
first_polyline[1,2] <- 4812088

# overwrite the first polyline in the data frame
sp.df@lines[[1]]@Lines[[1]]@coords <- first_polyline

# convert back
df.modified <- arc.sp2data(sp.df)
output <- file.path(getwd(), 'r_test.gdb', 'polyline_modified')
arc.write(output, df.modified)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍


Let us know if that does what you need.

Cheers,

Shaun

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

The link is not very useful.  In any event a polyline could be a list of lists of course since each part of a polyline would be represented as a list (vector) of points so the list of list structure is not strange

0 Kudos
AnnieO_Donnell
Deactivated User

The link describes exactly what I am trying to do - pull data from ArcGIS into an R environment, modify this in some way in R and push data back to ArcGIS.

When I try to do this with a Polyline shape I get a length of shape error (same as the link). The link I posted describes how to solve this for point line only:

```

> d<-arc.open("c:\\data\\points.shp")> df<-arc.select(d)> g<-arc.shape(df)> dfxy<-data.frame(df, g$x, g$y) #filter rows> arc.write("c:\\temp\\new_points.shp", dfxy, coords=list(dfxy$g.x, dfxy$g.y), shape_info=arc.shapeinfo(g))

```

But this does not work for Polyline. I understand why the polyline is a list of vectors, that makes sense - but how do we access these co-ordinates and apply the appropriate filtering to the modified data (as above) so it can be pushed back to ArcGIS without erroring?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Sorry Annie, complete help is a bit away for me... still on my reading list but the manual page 14 gives some suggestions but you may want to wait for more concrete examples, 

0 Kudos
ShaunWalbridge
Esri Regular Contributor

Hello Annie O'Donnell‌,

The trick posted on the GitHub issue works nicely because the dimensionality of a point can easily be represented in a tabular form, by expanding the X and Y coordinates into two columns. For higher dimension objects like polyline, it doesn't work directly because of the dimensionality difference. While you could build up lists to represent this data, I think it'll be less work to convert the data into sp class objects. For Polyline, you'll want SpatialLinesDataFrame (sp documentation), or to manually populate a SpatialLines object from a collection of lists. Here's a short example updating the first coordinate of the first polyline, and saving it back to a new FGDB feature class:

library(arcgisbinding)
arc.check_product()

setwd('c:/temp')
input <- file.path(getwd(), 'r_test.gdb', 'polyline_example')

d <- arc.open(input)
df <- arc.select(d)
g <- arc.shape(df)
sp.df <- arc.data2sp(df)

first_polyline <- sp.df@lines[[1]]@Lines[[1]]@coords
# update the first Y coordinate
first_polyline[1,2] <- 4812088

# overwrite the first polyline in the data frame
sp.df@lines[[1]]@Lines[[1]]@coords <- first_polyline

# convert back
df.modified <- arc.sp2data(sp.df)
output <- file.path(getwd(), 'r_test.gdb', 'polyline_modified')
arc.write(output, df.modified)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍


Let us know if that does what you need.

Cheers,

Shaun