Creating and combining polygons in R with sfGetting TopologyException: Input geom 1 is invalid which is due...

How can a kingdom keep the secret of a missing monarch from the public?

What dissuades people from lying about where they live in order to reduce state income taxes?

How can I differentiate duration vs starting time

What happens if you declare more than $10,000 at the US border?

Ramanujan's radical and how we define an infinite nested radical

How to achieve physical gender equality?

Are encryption algorithms with fixed-point free permutations inherently flawed?

How can I portray body horror and still be sensitive to people with disabilities?

Why didn't Lorentz conclude that no object can go faster than light?

How can changes in personality/values of a person who turned into a vampire be explained?

Is layered encryption more secure than long passwords?

What is the difference between crontab -e and nano /etc/crontab?

Short story where Earth is given a racist governor who likes species of a certain color

Why do we divide Permutations to get to Combinations?

How do I handle a blinded enemy which wants to attack someone it's sure is there?

Is opening a file faster than reading variable content?

Boss asked me to sign a resignation paper without a date on it along with my new contract

Identical projects by students at two different colleges: still plagiarism?

Sauna: Wood does not feel so hot

Why is quixotic not Quixotic (a proper adjective)?

How do I know my password or backup information is not being shared when creating a new wallet?

Build ASCII Podiums

Found a major flaw in paper from home university – to which I would like to return

How should I ship cards?



Creating and combining polygons in R with sf


Getting TopologyException: Input geom 1 is invalid which is due to self-intersection in R?using shapely: translating between Polygons and MultiPolygonsWrong projection high quality shapefile for the coastline of Helsinki region (Finland)Extract all the polygon coordinates from a SpatialPolygonsDataframe?2D polygons with height attribute to 3D features for ArcGIS Online“Hole” argument ignored in call to Polygon functionArea-weighted average raster values within each SpatialPolygonsDataFrame polygon (R)Unique SpatialPolygons object with multiple polygons slots in Rsf lines to polygons with holesr - create multipolygon from overlapping polygons using sf packageSelecting all polygons along northern edge of shapefile in R













0















I'm getting a bit confused by a few supposedly basic tasks involving polygons in R. I'm tying to keep everything in sf, but if I need to switch to sp that's ok.



Firstly, I'm creating some polygons from scratch using coordinates (lat/long). I'm calling st_polygon on each table of coordinates and joining the results into a list. If we call that list polys then



class(polys[[1]])
[1] "XY" "POLYGON" "sfg"

polys[[1]]
POLYGON ((-68.48597 -54.50921, -68.46605 -54.50971, [...]


I'm also reading in another polygon from a shapefile:



external_polys <-  st_read("C:/.../shapefile-polygons.shp")

class(external_polys)
[1] "sf" "data.frame"

external_polys
Simple feature collection with 4711 features and 28 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -179.9196 ymin: -50.75335 xmax: 179.2308 ymax: 83.73
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
[...]


Ultimately I want to be able to write a shapefile which simply combines all these polygons. It will be used simply as a masking layer, so I don't really care about carrying attributes forward. There are overlapping areas, and I will want to dissolve these (just want to know whether any particular place is covered by a polygon or not). However, I'm struggling to do this.



At the moment I've got:



polys_combined <- reduce(polys, c) %>% st_combine() %>% st_set_crs(4326)

# Add these to external polygons

all_polygons_combined <- st_union(polys_combined, external_polygons)


But this throws an error Input geom 0 is invalid: Self-intersection at or near point 12.500000000000057 32.080000000000041 at 12.500000000000057 32.080000000000041.



Thoughts? I'm sure there's better ways of doing each step... I'd particularly like a neater way of going from the coordinates to a simple features collection of polygons.










share|improve this question























  • It's hard to tell what the problem is without the actual data, but judging from the error message you have invalid geometries in your data. Running sf::st_buffer(x, dist = 0) could possibly solve your problems (see also gis.stackexchange.com/questions/163445/…)

    – karpfen
    4 hours ago











  • I don't know if this is relevant, but I can plot/write to shapefile both polys_combined and external_polygons fine, so separately their geometries seem to be ok?

    – Tim K
    4 hours ago











  • I wouldn't rely on that. You could also check your polygons with st_is_valid and work from there. (cran.r-project.org/web/packages/sf/vignettes/sf3.html)

    – karpfen
    4 hours ago
















0















I'm getting a bit confused by a few supposedly basic tasks involving polygons in R. I'm tying to keep everything in sf, but if I need to switch to sp that's ok.



Firstly, I'm creating some polygons from scratch using coordinates (lat/long). I'm calling st_polygon on each table of coordinates and joining the results into a list. If we call that list polys then



class(polys[[1]])
[1] "XY" "POLYGON" "sfg"

polys[[1]]
POLYGON ((-68.48597 -54.50921, -68.46605 -54.50971, [...]


I'm also reading in another polygon from a shapefile:



external_polys <-  st_read("C:/.../shapefile-polygons.shp")

class(external_polys)
[1] "sf" "data.frame"

external_polys
Simple feature collection with 4711 features and 28 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -179.9196 ymin: -50.75335 xmax: 179.2308 ymax: 83.73
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
[...]


Ultimately I want to be able to write a shapefile which simply combines all these polygons. It will be used simply as a masking layer, so I don't really care about carrying attributes forward. There are overlapping areas, and I will want to dissolve these (just want to know whether any particular place is covered by a polygon or not). However, I'm struggling to do this.



At the moment I've got:



polys_combined <- reduce(polys, c) %>% st_combine() %>% st_set_crs(4326)

# Add these to external polygons

all_polygons_combined <- st_union(polys_combined, external_polygons)


But this throws an error Input geom 0 is invalid: Self-intersection at or near point 12.500000000000057 32.080000000000041 at 12.500000000000057 32.080000000000041.



Thoughts? I'm sure there's better ways of doing each step... I'd particularly like a neater way of going from the coordinates to a simple features collection of polygons.










share|improve this question























  • It's hard to tell what the problem is without the actual data, but judging from the error message you have invalid geometries in your data. Running sf::st_buffer(x, dist = 0) could possibly solve your problems (see also gis.stackexchange.com/questions/163445/…)

    – karpfen
    4 hours ago











  • I don't know if this is relevant, but I can plot/write to shapefile both polys_combined and external_polygons fine, so separately their geometries seem to be ok?

    – Tim K
    4 hours ago











  • I wouldn't rely on that. You could also check your polygons with st_is_valid and work from there. (cran.r-project.org/web/packages/sf/vignettes/sf3.html)

    – karpfen
    4 hours ago














0












0








0








I'm getting a bit confused by a few supposedly basic tasks involving polygons in R. I'm tying to keep everything in sf, but if I need to switch to sp that's ok.



Firstly, I'm creating some polygons from scratch using coordinates (lat/long). I'm calling st_polygon on each table of coordinates and joining the results into a list. If we call that list polys then



class(polys[[1]])
[1] "XY" "POLYGON" "sfg"

polys[[1]]
POLYGON ((-68.48597 -54.50921, -68.46605 -54.50971, [...]


I'm also reading in another polygon from a shapefile:



external_polys <-  st_read("C:/.../shapefile-polygons.shp")

class(external_polys)
[1] "sf" "data.frame"

external_polys
Simple feature collection with 4711 features and 28 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -179.9196 ymin: -50.75335 xmax: 179.2308 ymax: 83.73
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
[...]


Ultimately I want to be able to write a shapefile which simply combines all these polygons. It will be used simply as a masking layer, so I don't really care about carrying attributes forward. There are overlapping areas, and I will want to dissolve these (just want to know whether any particular place is covered by a polygon or not). However, I'm struggling to do this.



At the moment I've got:



polys_combined <- reduce(polys, c) %>% st_combine() %>% st_set_crs(4326)

# Add these to external polygons

all_polygons_combined <- st_union(polys_combined, external_polygons)


But this throws an error Input geom 0 is invalid: Self-intersection at or near point 12.500000000000057 32.080000000000041 at 12.500000000000057 32.080000000000041.



Thoughts? I'm sure there's better ways of doing each step... I'd particularly like a neater way of going from the coordinates to a simple features collection of polygons.










share|improve this question














I'm getting a bit confused by a few supposedly basic tasks involving polygons in R. I'm tying to keep everything in sf, but if I need to switch to sp that's ok.



Firstly, I'm creating some polygons from scratch using coordinates (lat/long). I'm calling st_polygon on each table of coordinates and joining the results into a list. If we call that list polys then



class(polys[[1]])
[1] "XY" "POLYGON" "sfg"

polys[[1]]
POLYGON ((-68.48597 -54.50921, -68.46605 -54.50971, [...]


I'm also reading in another polygon from a shapefile:



external_polys <-  st_read("C:/.../shapefile-polygons.shp")

class(external_polys)
[1] "sf" "data.frame"

external_polys
Simple feature collection with 4711 features and 28 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -179.9196 ymin: -50.75335 xmax: 179.2308 ymax: 83.73
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
[...]


Ultimately I want to be able to write a shapefile which simply combines all these polygons. It will be used simply as a masking layer, so I don't really care about carrying attributes forward. There are overlapping areas, and I will want to dissolve these (just want to know whether any particular place is covered by a polygon or not). However, I'm struggling to do this.



At the moment I've got:



polys_combined <- reduce(polys, c) %>% st_combine() %>% st_set_crs(4326)

# Add these to external polygons

all_polygons_combined <- st_union(polys_combined, external_polygons)


But this throws an error Input geom 0 is invalid: Self-intersection at or near point 12.500000000000057 32.080000000000041 at 12.500000000000057 32.080000000000041.



Thoughts? I'm sure there's better ways of doing each step... I'd particularly like a neater way of going from the coordinates to a simple features collection of polygons.







r polygon polygonize sf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









Tim KTim K

1734




1734













  • It's hard to tell what the problem is without the actual data, but judging from the error message you have invalid geometries in your data. Running sf::st_buffer(x, dist = 0) could possibly solve your problems (see also gis.stackexchange.com/questions/163445/…)

    – karpfen
    4 hours ago











  • I don't know if this is relevant, but I can plot/write to shapefile both polys_combined and external_polygons fine, so separately their geometries seem to be ok?

    – Tim K
    4 hours ago











  • I wouldn't rely on that. You could also check your polygons with st_is_valid and work from there. (cran.r-project.org/web/packages/sf/vignettes/sf3.html)

    – karpfen
    4 hours ago



















  • It's hard to tell what the problem is without the actual data, but judging from the error message you have invalid geometries in your data. Running sf::st_buffer(x, dist = 0) could possibly solve your problems (see also gis.stackexchange.com/questions/163445/…)

    – karpfen
    4 hours ago











  • I don't know if this is relevant, but I can plot/write to shapefile both polys_combined and external_polygons fine, so separately their geometries seem to be ok?

    – Tim K
    4 hours ago











  • I wouldn't rely on that. You could also check your polygons with st_is_valid and work from there. (cran.r-project.org/web/packages/sf/vignettes/sf3.html)

    – karpfen
    4 hours ago

















It's hard to tell what the problem is without the actual data, but judging from the error message you have invalid geometries in your data. Running sf::st_buffer(x, dist = 0) could possibly solve your problems (see also gis.stackexchange.com/questions/163445/…)

– karpfen
4 hours ago





It's hard to tell what the problem is without the actual data, but judging from the error message you have invalid geometries in your data. Running sf::st_buffer(x, dist = 0) could possibly solve your problems (see also gis.stackexchange.com/questions/163445/…)

– karpfen
4 hours ago













I don't know if this is relevant, but I can plot/write to shapefile both polys_combined and external_polygons fine, so separately their geometries seem to be ok?

– Tim K
4 hours ago





I don't know if this is relevant, but I can plot/write to shapefile both polys_combined and external_polygons fine, so separately their geometries seem to be ok?

– Tim K
4 hours ago













I wouldn't rely on that. You could also check your polygons with st_is_valid and work from there. (cran.r-project.org/web/packages/sf/vignettes/sf3.html)

– karpfen
4 hours ago





I wouldn't rely on that. You could also check your polygons with st_is_valid and work from there. (cran.r-project.org/web/packages/sf/vignettes/sf3.html)

– karpfen
4 hours ago










0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313038%2fcreating-and-combining-polygons-in-r-with-sf%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313038%2fcreating-and-combining-polygons-in-r-with-sf%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Щит и меч (фильм) Содержание Названия серий | Сюжет |...

Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

Meter-Bus Содержание Параметры шины | Стандартизация |...