TransWikia.com

Measuring distance from centroid to polygon's boundaries in R

Geographic Information Systems Asked on February 5, 2021

I’m still new to raster and sf and I’m having some trouble getting a distance between a point and a polygon for every time period.

I have a sf object with MULTIPOLYGONS that refers to flood episodes from 1800’s onwards and I have another sf object, with information on local authorities for each year (1990-2019). What I want to do is to get – for each year – the distance between the floods that entered the local authority’s boundaries and its centroid.

dput of the flood object:

dput(rec_simp[0:15,])

    structure(list(rec_out_id = c(5850L, 387L, 388L, 432L, 433L, 
434L, 13895L, 485L, 494L, 486L, 487L, 488L, 489L, 490L, 493L), 
    rec_grp_id = c(1529L, 2995L, 2995L, 2837L, 2837L, 2837L, 
    1528L, 2832L, 2838L, 2832L, 2832L, 2831L, 2831L, 2211L, 2838L
    ), name = c("1991 Flood Event", "June 2007 Event", "June 2007 Event", 
    "20th July 2007 River Teme (Upper)", "20th July 2007 River Teme (Upper)", 
    "20th July 2007 River Teme (Upper)", "Derwent March 1999 Event", 
    "Hatherley Brook 014 (July 07)", "River Chelt 020 (July 07)", 
    "Hatherley Brook 010 (July 07)", "Hatherley Brook 013 (July 07)", 
    "River Twyver 015 (July 07)", "River Twyver 012 (July 07)", 
    "06OctoberAutumn1993", "River Chelt 001 (July 07)"), start_date = c("1991/02/21 00:00:00", 
    "2007/06/25 00:00:00", "2007/06/25 00:00:00", "2007/07/21 00:00:00", 
    "2007/07/21 00:00:00", "2007/07/21 00:00:00", "1999/03/02 00:00:00", 
    "2007/07/20 00:00:00", "2007/07/20 00:00:00", "2007/07/20 00:00:00", 
    "2007/07/20 00:00:00", "2007/07/20 00:00:00", "2007/07/20 00:00:00", 
    "1993/01/01 00:00:00", "2007/07/20 00:00:00"), end_date = structure(c(7727, 
    13692, 13692, 13728, 13728, 13728, 10666, 13715, 13715, 13715, 
    13715, 13715, 13715, 8746, 13715), class = "Date"), flood_src = c("unknown", 
    "ordinary watercourse", "ordinary watercourse", "main river", 
    "main river", "main river", "unknown", "main river", "main river", 
    "main river", "main river", "main river", "main river", "main river", 
    "main river"), flood_caus = c("overtopping of defences", 
    "channel capacity exceeded (no raised defences)", "channel capacity exceeded (no raised defences)", 
    "other", "other", "other", "overtopping of defences", "channel capacity exceeded (no raised defences)", 
    "channel capacity exceeded (no raised defences)", "channel capacity exceeded (no raised defences)", 
    "channel capacity exceeded (no raised defences)", "channel capacity exceeded (no raised defences)", 
    "channel capacity exceeded (no raised defences)", "channel capacity exceeded (no raised defences)", 
    "channel capacity exceeded (no raised defences)"), fm_status = c("considered and accepted", 
    "considered and accepted", "considered and accepted", "considered and accepted", 
    "considered and accepted", "considered and accepted", "considered and accepted", 
    "considered and accepted", "considered and accepted", "considered and accepted", 
    "considered and accepted", "considered and accepted", "considered and accepted", 
    "considered and accepted", "considered and accepted"), hfm_status = c("considered and accepted", 
    "considered and accepted", "considered and accepted", "considered and accepted", 
    "considered and accepted", "considered and accepted", "considered and accepted", 
    "considered and accepted", "considered and accepted", "considered and accepted", 
    "considered and accepted", "considered and accepted", "considered and accepted", 
    "considered and accepted", "considered and accepted"), data_src = c("Survey", 
    "Survey", "Survey", "Survey", "Survey", "Survey", "Survey", 
    "Survey", "Survey", "Survey", "Survey", "Survey", "Survey", 
    "Survey", "Survey"), fluvial_f = c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L), coastal_f = c(0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), tidal_f = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    data_prov = c("Environment Agency", "Consultants", "Consultants", 
    "Consultants", "Consultants", "Consultants", "Consultants", 
    "Consultants", "Consultants", "Consultants", "Consultants", 
    "Consultants", "Consultants", "Consultants", "Consultants"
    ), data_qual = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), Shape_Length = c(20760.0862653932, 5252.55695170499, 1351.19839926208, 
    3035.78479345996, 292.111553478117, 13065.1591983069, 33868.8400127573, 
    1965.8116220814, 182.660675692031, 608.064648572066, 5726.6767189647, 
    293.667874827532, 627.095371022178, 18794.9886947699, 1353.73265052405
    ), Shape_Area = c(1237802.4776605, 103779.389935562, 55291.5354929778, 
    69156.0770502891, 1965.0632499598, 1466803.0281043, 1416900.42985681, 
    25080.6327434951, 1146.83835103637, 4983.13520000008, 197481.329154195, 
    1969.34710001101, 10711.6358285996, 408274.532087206, 33454.8197105237
    ), geometry = structure(list(structure(list(list(structure(c(4714322.40975255, 
    4716145.71145385, 4716141.55143781, 4717427.56364775, 4716324.5947451, 
    4716461.10167658, 4714322.40975255, 9174996.69745875, 9175917.62285533, 
    9177849.85669808, 9177498.59162881, 9177479.8209956, 9175477.50291387, 
    9174996.69745875), .Dim = c(7L, 2L))), list(structure(c(4716802.0864433, 
    4717252.6371426, 4717597.72146915, 4716802.0864433, 9175230.79164326, 
    9176006.03207018, 9174535.15945656, 9175230.79164326), .Dim = c(4L, 
    2L)))), class = c("XY", "MULTIPOLYGON", "sfg")), structure(list(), class = c("XY", 
    "POLYGON", "sfg")), structure(list(), class = c("XY", "POLYGON", 
    "sfg")), structure(list(), class = c("XY", "POLYGON", "sfg"
    )), structure(list(), class = c("XY", "POLYGON", "sfg")), 
        structure(list(structure(c(4916430.99622511, 4915832.129385, 
        4917260.9564723, 4916430.99622511, 9014504.98522568, 
        9016188.32618782, 9019526.22287062, 9014504.98522568), .Dim = c(4L, 
        2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
            list(structure(c(4699944.80708654, 4700006.75806449, 
            4699865.60722585, 4699382.14438072, 4700401.29451328, 
            4701517.9455769, 4702006.65011483, 4701684.1262684, 
            4700509.45768516, 4701081.27791338, 4699944.80708654, 
            9218585.4957194, 9218525.7511143, 9218676.65160039, 
            9219233.06595353, 9218437.62138694, 9220254.68749718, 
            9217855.92841653, 9219406.25470806, 9218177.22178006, 
            9217277.30001825, 9218585.4957194), .Dim = c(11L, 
            2L))), list(structure(c(4699089.36718683, 4696893.93971212, 
            4698990.84856102, 4699089.36718683, 9219410.40506698, 
            9218517.3317958, 9219505.39988582, 9219410.40506698
            ), .Dim = c(4L, 2L)))), class = c("XY", "MULTIPOLYGON", 
        "sfg")), structure(list(), class = c("XY", "POLYGON", 
        "sfg")), structure(list(), class = c("XY", "POLYGON", 
        "sfg")), structure(list(), class = c("XY", "POLYGON", 
        "sfg")), structure(list(), class = c("XY", "POLYGON", 
        "sfg")), structure(list(), class = c("XY", "POLYGON", 
        "sfg")), structure(list(), class = c("XY", "POLYGON", 
        "sfg")), structure(list(), class = c("XY", "POLYGON", 
        "sfg")), structure(list(), class = c("XY", "POLYGON", 
        "sfg"))), class = c("sfc_GEOMETRY", "sfc"), precision = 0, bbox = structure(c(xmin = 4696893.93971212, 
    ymin = 9014504.98522568, xmax = 4917260.9564723, ymax = 9220254.68749718
    ), class = "bbox"), crs = structure(list(input = "EPSG:32617", 
        wkt = "PROJCRS["WGS 84 / UTM zone 17N",n    BASEGEOGCRS["WGS 84",n        DATUM["World Geodetic System 1984",n            ELLIPSOID["WGS 84",6378137,298.257223563,n                LENGTHUNIT["metre",1]]],n        PRIMEM["Greenwich",0,n            ANGLEUNIT["degree",0.0174532925199433]],n        ID["EPSG",4326]],n    CONVERSION["UTM zone 17N",n        METHOD["Transverse Mercator",n            ID["EPSG",9807]],n        PARAMETER["Latitude of natural origin",0,n            ANGLEUNIT["degree",0.0174532925199433],n            ID["EPSG",8801]],n        PARAMETER["Longitude of natural origin",-81,n            ANGLEUNIT["degree",0.0174532925199433],n            ID["EPSG",8802]],n        PARAMETER["Scale factor at natural origin",0.9996,n            SCALEUNIT["unity",1],n            ID["EPSG",8805]],n        PARAMETER["False easting",500000,n            LENGTHUNIT["metre",1],n            ID["EPSG",8806]],n        PARAMETER["False northing",0,n            LENGTHUNIT["metre",1],n            ID["EPSG",8807]]],n    CS[Cartesian,2],n        AXIS["(E)",east,n            ORDER[1],n            LENGTHUNIT["metre",1]],n        AXIS["(N)",north,n            ORDER[2],n            LENGTHUNIT["metre",1]],n    USAGE[n        SCOPE["unknown"],n        AREA["World - N hemisphere - 84°W to 78°W - by country"],n        BBOX[0,-84,84,-78]],n    ID["EPSG",32617]]"), class = "crs"), classes = c("MULTIPOLYGON", 
    "POLYGON", "POLYGON", "POLYGON", "POLYGON", "POLYGON", "MULTIPOLYGON", 
    "POLYGON", "POLYGON", "POLYGON", "POLYGON", "POLYGON", "POLYGON", 
    "POLYGON", "POLYGON"), n_empty = 12L), start_date2 = structure(c(7721, 
    13689, 13689, 13715, 13715, 13715, 10652, 13714, 13714, 13714, 
    13714, 13714, 13714, 8401, 13714), class = "Date"), year = c(1991, 
    2008, 2008, 2008, 2008, 2008, 1999, 2008, 2008, 2008, 2008, 
    2008, 2008, 1993, 2008)), sf_column = "geometry", agr = structure(c(rec_out_id = NA_integer_, 
rec_grp_id = NA_integer_, name = NA_integer_, start_date = NA_integer_, 
end_date = NA_integer_, flood_src = NA_integer_, flood_caus = NA_integer_, 
fm_status = NA_integer_, hfm_status = NA_integer_, data_src = NA_integer_, 
fluvial_f = NA_integer_, coastal_f = NA_integer_, tidal_f = NA_integer_, 
data_prov = NA_integer_, data_qual = NA_integer_, Shape_Length = NA_integer_, 
Shape_Area = NA_integer_, start_date2 = NA_integer_, year = NA_integer_
), .Label = c("constant", "aggregate", "identity"), class = "factor"), row.names = c(NA, 
15L), class = c("sf", "data.frame"))

dput of the local authority object:

dput(el9018[0:15,])
      structure(list(name = c("Hartlepool", "Hartlepool", "Hartlepool", 
"Hartlepool", "Hartlepool", "Hartlepool", "Hartlepool", "Hartlepool", 
"Hartlepool", "Hartlepool", "Hartlepool", "Hartlepool", "Hartlepool", 
"Hartlepool", "Hartlepool"), year = c(1996, 2008, 2006, 1990, 
2004, 1991, 1995, 2000, 1994, 2003, 1999, 2002, 1993, 1998, 2007
), st_areasha = c(93865248.283153, 93865248.283153, 93865248.283153, 
93865248.283153, 93865248.283153, 93865248.283153, 93865248.283153, 
93865248.283153, 93865248.283153, 93865248.283153, 93865248.283153, 
93865248.283153, 93865248.283153, 93865248.283153, 93865248.283153
), st_lengths = c(71094.4383027322, 71094.4383027322, 71094.4383027322, 
71094.4383027322, 71094.4383027322, 71094.4383027322, 71094.4383027322, 
71094.4383027322, 71094.4383027322, 71094.4383027322, 71094.4383027322, 
71094.4383027322, 71094.4383027322, 71094.4383027322, 71094.4383027322
), geometry = list(structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(4620048.87342784, 
4625136.14033251, 4626169.97135356, 4624903.68499877, 4625666.98306727, 
4627236.26049621, 4630383.60094198, 4632181.9934407, 4634222.13636813, 
4633733.53415866, 4634792.40551378, 4632805.240467, 4633813.29962425, 
4632481.55986368, 4629910.70167173, 4631429.43959516, 4628885.5811266, 
4628828.22764404, 4627103.76491929, 4624790.90448235, 4623373.10700597, 
4623774.92804902, 4620554.19293119, 4620048.87342784, 9196890.6003961, 
9203047.88855518, 9203113.63269664, 9201599.80199553, 9202139.15172575, 
9200864.14932645, 9201479.63576469, 9203092.42994029, 9202073.30466573, 
9200275.62185513, 9198288.71461306, 9196674.89026299, 9194785.61668234, 
9191674.23504443, 9190853.39755471, 9189011.54061077, 9185750.32340507, 
9189094.46102474, 9188949.81426043, 9190824.07050973, 9190050.68152116, 
9191979.44980661, 9193807.72767383, 9196890.6003961), .Dim = c(24L, 
2L))), class = c("XY", "POLYGON", "sfg"))), centroid = list(structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")), structure(c(4627696.7784981, 
9195871.63743641), class = c("XY", "POINT", "sfg")))), sf_column = "geometry", agr = structure(c(NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_), .Names = c("name", 
"year", "st_areasha", "st_lengths", NA), .Label = c("constant", 
"aggregate", "identity"), class = "factor"), row.names = c(NA, 
15L), class = c("sf", "data.frame"))

I got the local authory’s centroid with:

 el9018_simp <- el9018_simp %>% 
  st_transform(32617)
el9018_simp$centroid <- st_centroid(el9018_simp$geometry)

Following Test intersections with time indicators in R,
I tried this for loop:

library(rgeos)
for(i in unique(el9018_simp$year)) {
    dat1_sub = rec_simp[rec_simp$year == i, ]
    el9018_sub = st_union(el9018_simp[el9018_simp$year == i, ])
    int = st_intersects(
        el9018_sub, 
        dat1_sub,
        sparse = T #keep all floods per year
    )
    for (j in int) {
      dist = gDistance(el9018_sub$centroid, int[j],byid=TRUE)
      el9018_simp$dist[el9018_simp$year == i] = dist
    }
    
}

I tried different iterations of this,but I get errors or something off.
Thanks!

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP