Extract one band data from Geotiff file and plot colored image based on one band values on folium map - raster

I am new to extracting data from Geotiff files.So this question will sound obvious.I have tiff file which I have got from api.It has 12 bands.This shows definition of what each band contains
What I want
After extenisve searching for days i am only able to do this.
enter image description here
Code which i have tried
driver=gdal.GetDriverByName('GTiFF')
driver.Register()
ds = gdal.Open('...../286d7a11-4abd-449e-856b-79959dfd1396.tif')
if ds is None:
print('Could not open')
geotransform = ds.GetGeoTransform()
proj = ds.GetProjection()
cols = ds.RasterXSize
rows = ds.RasterYSize
xmin=geotransform[0]
ymax=geotransform[3]
xmax=xmin+cols*geotransform[1]
ymin=ymax+rows*geotransform[5]
centerx=(xmin+xmax)/2
centery=(ymin+ymax)/2
bands = ds.RasterCount
band10 = ds.GetRasterBand(10)
array = band10.ReadAsArray()
map= folium.Map(location=[centery, centerx], zoom_start=7,tiles='openstreetmap')
image = folium.raster_layers.ImageOverlay(
dataimage,opacity=0.8, bounds=[[ymin,xmin],[ymax,xmax]]
)
image.add_to(map)
map.fit_bounds(map.get_bounds(), padding=(10, 10))
return map

Related

Is there a way to do a spatial join between point layer based on lat and lon with heat map created as a result of info?

I have a dataset with geometry through the sf package included with lat and lon information. I also have a heat map created as a result of the dataset
Overlay<-stat_density2d(aes(x = df$LONGITUDE, y = fd$LATITUDE, fill = ..density..), geom = 'tile', contour = F, alpha = .5)
Is there a way to join the heat map density color values into the point file?
st_join(Shots1B,Overlay)
gives me an error
Error in st_join.sf(Shots1B, Overlay) :
second argument should be of class sf: maybe revert the first two arguments?
Reproductive dataset to follow the process. Overlay is based on the result of this dataset here.
structure(list(LATITUDE = c(40.68358, 40.69754, 40.843464, 40.692547,
40.626457, 40.526894, 40.840775, 40.694035, 40.857365, 40.698807,
40.71402, 40.815, 40.55079, 40.655903, 40.890076, 40.650402,
40.79335, 40.72538, 40.75184, 40.649788, 40.686928, 40.712963,
40.801285, 40.633976, 40.670296, 40.66423, 40.817696, 40.668495,
40.841087, 40.70955, 40.733376, 40.700356, 40.83801, 40.66584,
40.761436, 40.74958, 40.73197, 40.76249, 40.668507, 40.638268,
40.696735, 40.870823, 40.574867, 40.866577, 40.775414, 40.84744,
40.542908, 40.78468, 40.632416, 40.714207, 40.727913, 40.854485,
40.698986, 40.841717, 40.861687, 40.691822, 40.856014, 40.83383,
40.68781, 40.642044, 40.69814, 40.64664, 40.680897, 40.760822,
40.74608, 40.626293, 40.767967, 40.673634, 40.579212, 40.57365,
40.73632, 40.619396, 40.820263, 40.601864, 40.810318, 40.666306,
40.708805, 40.826424, 40.63174, 40.727146, 40.67253, 40.702335,
40.587894, 40.67922, 40.65047, 40.836555, 40.870056, 40.579372,
40.805138, 40.85968, 40.605595, 40.819214, 40.827972, 40.66496,
40.719177, 40.748825, 40.733597, 40.54048, 40.738403, 40.68039,
40.817627, 40.751446, 40.76161, 40.689648, 40.596977, 40.63864,
40.565254, 40.655895, 40.68821, 40.71649, 40.876785, 40.86367,
40.835827, 40.793396, 40.84827, 40.656273, 40.693462, 40.66725,
40.844105, 40.651707, 40.680496, 40.834415, 40.7357, 40.771038,
40.69484, 40.785774, 40.733017, 40.709023, 40.692886, 40.620487,
40.618595, 40.803787, 40.82319, 40.680088, 40.827927, 40.66895,
40.879055, 40.67043, 40.875874, 40.675037, 40.767582, 40.734352,
40.63083, 40.63532, 40.714073, 40.702194, 40.764362, 40.69496,
40.79656, 40.805016, 40.66406, 40.7963, 40.66563, 40.680477,
40.737785, 40.778606, 40.75868, 40.856045, 40.880257, 40.60677,
40.695683, 40.667236, 40.8351, 40.633682, 40.698116, 40.84747,
40.8047, 40.762676, 40.7158, 40.75584, 40.772102, 40.681602,
40.62677, 40.707493, 40.8252, 40.854115, 40.768875, 40.629707,
40.72654, 40.634415, 40.66937, 40.89466, 40.669067, 40.681484,
40.82433, 40.856606, 40.65785, 40.62764, 40.58401, 40.71791,
40.780437, 40.73973, 40.7952, 40.694794, 40.614063, 40.633152,
40.612736, 40.70166, 40.80641, 40.762234, 40.863647, 40.576626,
40.60118, 40.64721, 40.681145, 40.57529, 40.786, 40.601128, 40.827923,
40.805824, 40.642776, 40.86674, 40.678375, 40.74209, 40.81228,
40.604195, 40.84383, 40.759163, 40.652927, 40.69097, 40.718864,
40.683174, 40.749744, 40.738316, 40.839382, 40.66806, 40.74715,
40.663776, 40.843903, 40.836296, 40.655285, 40.70166, 40.64606,
40.72119, 40.708363, 40.674004, 40.729176, 40.86832, 40.598515,
40.695004, 40.72773, 40.704563, 40.66807, 40.66944, 40.684082,
40.69349, 40.765266, 40.74613, 40.74708, 40.87482, 40.70399,
40.649788, 40.69507, 40.788673, 40.847897, 40.68896, 40.695377,
40.880657, 40.828114, 40.781265, 40.848736, 40.65989, 40.748436,
40.61033, 40.752556, 40.829697, 40.718826, 40.65241, 40.852673,
40.851555, 40.707928, 40.891876, 40.58947, 40.74668, 40.85814,
40.708626, 40.73464, 40.62855, 40.65563, 40.687046, 40.70326,
40.633114, 40.62046, 40.75964, 40.64254, 40.783146, 40.705452,
40.74425, 40.75348, 40.84307, 40.620914, 40.80889, 40.78847,
40.712776, 40.75868, 40.74661, 40.835705, 40.688404, 40.781715,
40.730644, 40.75218, 40.731422, 40.761234, 40.668976, 40.637276,
40.788685, 40.87356, 40.795006, 40.820095, 40.594334, 40.666306,
40.673008, 40.583626, 40.874474, 40.633995, 40.772327, 40.704937,
40.653873, 40.677917, 40.59857, 40.809563, 40.68836, 40.666737,
40.713173, 40.73006, 40.652317, 40.76122, 40.588722, 40.643456,
40.865532, 40.67612, 40.620663, 40.72166, 40.733723, 40.745686,
40.875294, 40.803555, 40.7605, 40.661995, 40.69045, 40.658672,
40.711227, 40.700485, 40.816555, 40.861862, 40.875793, 40.68657,
40.654705, 40.637054, 40.6191, 40.734566, 40.714912, 40.74734,
40.6963, 40.63598, 40.724358, 40.586277, 40.671932, 40.650703,
40.61378, 40.727375, 40.573204, 40.671604, 40.740276, 40.684,
40.704494, 40.845642, 40.82681, 40.681168, 40.662476, 40.64739,
40.687138, 40.865143, 40.866673, 40.72313, 40.674934, 40.708363,
40.739525, 40.637997, 40.750965, 40.671585, 40.694294, 40.810173,
40.694748, 40.687103, 40.861744, 40.741074, 40.67875, 40.666943,
40.6635, 40.827824, 40.575832, 40.730366, 40.640945, 40.784237,
40.76803, 40.669823, 40.659336, 40.616093, 40.763546), LONGITUDE = c(-73.97617,
-73.98312, -73.836, -73.990974, -73.918, -74.16728, -73.87246,
-73.72679, -73.84657, -73.91837, -73.74827, -73.89402, -74.20098,
-73.89817, -73.819855, -73.89422, -73.97275, -74.00011, -73.90358,
-73.9622, -73.920815, -73.93647, -73.95394, -74.02211, -73.997604,
-73.919106, -73.922615, -73.925606, -73.86447, -73.95887, -73.86665,
-73.95732, -73.87329, -73.75551, -73.76995, -73.86541, -73.78651,
-73.839584, -73.779625, -73.93187, -73.93481, -73.8721, -74.00069,
-73.8722, -73.91984, -73.89968, -74.15579, -73.80911, -73.94724,
-73.92817, -73.873245, -73.854645, -73.91671, -73.94435, -73.82435,
-73.92223, -73.91213, -73.921234, -73.9237, -73.98124, -73.89111,
-73.9246, -73.95118, -73.99832, -73.974945, -74.01572, -73.96822,
-73.89294, -73.976265, -74.11252, -73.85631, -73.969574, -73.92976,
-74.00232, -73.943634, -73.79171, -73.92577, -73.85868, -73.96793,
-73.954735, -73.798386, -73.89073, -73.95504, -73.90405, -73.917366,
-73.94306, -73.83222, -74.16948, -73.945244, -73.90427, -73.98404,
-73.84662, -73.88707, -73.82226, -73.79223, -73.96984, -73.91062,
-74.153404, -73.93864, -73.94956, -73.92366, -74.001434, -73.97076,
-73.9184, -73.97324, -74.02245, -74.1301, -73.898224, -73.96583,
-73.98484, -73.87446, -73.86741, -73.89068, -73.94043, -73.88312,
-73.90731, -73.965485, -73.88799, -73.923065, -73.93121, -73.821365,
-73.92854, -73.90377, -73.83413, -73.98391, -73.97052, -73.8852,
-73.757835, -73.832184, -74.029305, -73.99847, -73.953896, -73.889496,
-73.94398, -73.90094, -73.9339, -73.87439, -73.928185, -73.84989,
-73.930534, -73.9109, -74.00849, -73.90736, -73.95033, -73.95087,
-73.93587, -73.96162, -73.946236, -73.97226, -73.92109, -73.73846,
-73.93829, -73.73921, -73.7921, -73.93496, -73.98163, -73.87552,
-73.90079, -73.843864, -73.759575, -73.741875, -73.770004, -73.8825,
-73.89868, -73.977325, -73.89134, -73.91243, -73.954346, -73.824486,
-73.99238, -73.763954, -73.95855, -73.946884, -73.94153, -73.867714,
-73.89091, -73.94898, -73.90486, -73.71589, -74.08535, -73.89523,
-73.86137, -73.9878, -73.85049, -73.874374, -73.92841, -73.91648,
-73.89022, -73.98587, -73.95341, -73.94989, -73.70625, -73.94623,
-73.98246, -74.01393, -73.97762, -74.01172, -73.961464, -73.94227,
-73.98987, -73.8918, -73.98478, -73.99098, -74.01531, -73.7923,
-73.97655, -73.84574, -73.997086, -73.93483, -73.954636, -74.02003,
-73.92873, -73.9265, -73.984985, -73.90941, -73.97218, -73.886375,
-73.988396, -73.959335, -73.94833, -73.97483, -73.87389, -73.884315,
-73.98773, -73.84531, -73.90394, -73.985504, -73.889915, -73.927284,
-73.87369, -73.93658, -73.961464, -74.01648, -73.761185, -73.87203,
-73.81881, -73.87898, -73.83317, -73.766464, -73.9525, -73.90674,
-73.91066, -73.80789, -73.883995, -73.90864, -73.97917, -73.81517,
-73.83605, -73.98135, -73.877, -73.85589, -73.9622, -73.80114,
-73.97136, -73.92499, -73.93326, -73.94921, -73.877625, -73.93107,
-73.97599, -73.93234, -73.90536, -73.984566, -73.95932, -73.92972,
-73.91313, -73.98424, -73.9264, -73.919106, -73.952446, -73.784294,
-73.8616, -73.80105, -73.9745, -73.895744, -73.94513, -73.87421,
-73.952835, -73.92596, -73.792114, -73.86474, -73.94928, -74.07641,
-73.95817, -73.87652, -73.97833, -73.78149, -73.7334, -73.980896,
-73.848076, -73.9753, -73.95581, -73.968895, -73.90601, -73.87552,
-73.86473, -73.88875, -73.93803, -73.823845, -73.97329, -73.85201,
-73.94641, -73.96389, -73.90668, -73.93177, -73.94386, -73.81853,
-73.9485, -73.955086, -73.990944, -73.79171, -73.97851, -73.98407,
-73.90031, -73.98138, -73.94531, -73.94932, -74.008156, -73.93861,
-73.9689, -73.92923, -73.96444, -73.90224, -73.90076, -73.710754,
-73.92752, -73.93056, -73.960464, -73.972725, -73.86238, -73.936005,
-74.1524, -73.888664, -73.72516, -73.97213, -73.9088, -73.91184,
-73.95699, -73.91959, -73.959435, -73.90019, -73.72826, -73.93637,
-73.91755, -73.91282, -73.85465, -73.776146, -74.00731, -73.98643,
-74.159615, -73.72269, -73.94784, -73.88673, -73.97545, -73.915146,
-73.742516, -73.9862, -73.85165, -73.920586, -74.07225, -73.90313,
-74.09711, -73.86907, -73.92782, -73.95031, -73.81743, -73.90211,
-73.85361, -73.92941, -73.768326, -73.92314, -73.75136, -73.87204,
-73.90896, -73.90614, -73.80186, -73.92401, -73.92512, -74.02136,
-73.94027, -73.99843, -73.74868, -73.95117, -73.73427, -73.89251,
-73.911804, -73.7258, -73.794, -73.890144, -73.94276, -73.91934,
-74.12409, -73.91388, -73.94852, -73.947075, -73.87722, -73.90981,
-73.92726, -74.14523, -73.88209), COLLISION_ID = c(4407147L,
4136992L, 4395664L, 4397513L, 4403773L, 4405244L, 4405914L, 4407366L,
4407778L, 4407461L, 4407407L, 4407900L, 4407760L, 4407746L, 4408143L,
4407638L, 4407958L, 4407885L, 4407616L, 4408038L, 4408224L, 4407392L,
4407765L, 4407821L, 4407971L, 4408071L, 4407430L, 4408259L, 4407592L,
4407674L, 4407708L, 4408396L, 4407152L, 4407862L, 4407636L, 4407792L,
4407853L, 4408205L, 4407945L, 4408118L, 4408242L, 4407563L, 4408098L,
4407169L, 4407798L, 4407797L, 4407349L, 4407994L, 4408032L, 4407478L,
4407924L, 4408315L, 4407892L, 4408280L, 4408403L, 4407753L, 4408003L,
4407497L, 4408229L, 4407525L, 4407817L, 4407539L, 4408306L, 4407830L,
4407282L, 4407688L, 4407701L, 4407728L, 4408052L, 4407849L, 4407320L,
4407291L, 4408200L, 4407649L, 4407802L, 4407345L, 4408356L, 4407245L,
4408057L, 4408332L, 4407785L, 4407929L, 4407425L, 4408080L, 4408123L,
4408290L, 4408412L, 4407757L, 4407770L, 4407554L, 4407873L, 4407502L,
4408044L, 4408165L, 4407544L, 4407277L, 4407834L, 4407338L, 4407397L,
4408109L, 4407683L, 4407829L, 4407496L, 4407609L, 4407689L, 4407861L,
4407350L, 4407721L, 4407381L, 4407653L, 4407896L, 4407914L, 4407512L,
4408427L, 4407532L, 4408086L, 4407856L, 4407729L, 4407789L, 4408129L,
4407576L, 4408193L, 4407643L, 4407906L, 4407414L, 4407623L, 4407436L,
4407952L, 4407761L, 4408063L, 4407388L, 4407766L, 4407901L, 4408104L,
4407486L, 4408264L, 4407678L, 4407355L, 4408155L, 4408271L, 4408380L,
4407598L, 4407866L, 4407452L, 4407809L, 4407393L, 4407453L, 4408214L,
4407824L, 4407431L, 4407632L, 4407841L, 4407096L, 4407658L, 4407933L,
4407976L, 4407714L, 4407373L, 4407568L, 4407707L, 4407692L, 4407865L,
4407154L, 4407637L, 4407354L, 4407793L, 4407432L, 4407946L, 4407852L,
4408249L, 4407987L, 4408215L, 4408027L, 4407389L, 4407562L, 4407794L,
4407779L, 4407997L, 4407408L, 4408313L, 4407747L, 4408402L, 4407702L,
4407762L, 4407297L, 4407321L, 4408100L, 4407505L, 4408047L, 4407805L,
4407697L, 4407367L, 4407837L, 4407548L, 4407820L, 4408150L, 4407842L,
4407673L, 4407278L, 4407459L, 4407514L, 4408367L, 4407313L, 4407396L,
4407611L, 4408053L, 4407905L, 4407642L, 4407556L, 4407774L, 4407360L,
4408139L, 4408223L, 4408075L, 4407967L, 4407875L, 4407915L, 4408258L,
4408033L, 4408308L, 4407869L, 4407732L, 4407602L, 4407959L, 4408397L,
4408067L, 4408279L, 4407723L, 4408198L, 4407501L, 4408119L, 4408359L,
4407816L, 4407543L, 4407801L, 4407346L, 4407769L, 4408093L, 4407475L,
4408113L, 4407833L, 4407923L, 4407424L, 4407741L, 4407891L, 4407647L,
4407909L, 4407439L, 4407604L, 4407752L, 4407380L, 4408037L, 4408416L,
4407549L, 4408002L, 4408227L, 4408262L, 4407897L, 4408187L, 4407975L,
4408164L, 4408085L, 4408250L, 4408028L, 4407580L, 4408302L, 4407400L,
4408128L, 4408194L, 4408289L, 4407953L, 4408391L, 4408204L, 4407870L,
4407524L, 4408353L, 4407445L, 4408056L, 4407327L, 4407784L, 4407928L,
4407454L, 4407372L, 4407444L, 4407756L, 4407620L, 4407412L, 4407633L,
4407119L, 4407660L, 4408154L, 4407773L, 4407567L, 4407938L, 4407713L,
4408208L, 4408043L, 4408233L, 4407990L, 4408079L, 4408217L, 4408331L,
4407698L, 4407825L, 4408122L, 4407838L, 4407948L, 4407843L, 4407515L,
4407312L, 4407314L, 4407489L, 4407656L, 4407332L, 4407506L, 4408401L,
4407677L, 4407359L, 4408266L, 4407696L, 4407261L, 4407857L, 4407490L,
4407788L, 4408379L, 4408103L, 4408076L, 4408049L, 4407527L, 4407836L,
4408274L, 4407547L, 4407815L, 4408375L, 4407279L, 4407331L, 4407686L,
4408398L, 4408066L, 4408309L, 4408068L, 4407691L, 4407353L, 4407433L,
4407679L, 4407566L, 4408026L, 4407651L, 4407296L, 4407826L, 4407368L,
4407804L, 4407242L, 4407858L, 4407795L, 4407347L, 4408059L, 4407715L,
4408114L, 4408312L, 4407922L, 4407342L, 4407748L, 4407619L, 4408297L,
4407763L, 4408005L, 4407504L, 4408231L, 4407049L, 4408106L, 4407542L,
4407523L, 4407428L, 4408257L, 4407595L, 4407672L, 4407832L, 4407384L,
4408387L, 4407790L, 4407612L, 4407775L, 4407864L, 4407379L, 4407156L,
4407634L, 4408389L, 4407724L, 4407942L, 4408197L, 4407847L, 4407403L,
4408120L, 4407703L, 4407668L, 4407570L, 4408101L, 4407800L, 4407917L
)), row.names = c(NA, 400L), class = "data.frame")

Convert set of coordinates in specyfic format into sf object (polygon) in R

I have data frame that contains variable with set of coordinates, which supposed to be a polygon. I’d like to try to convert it into sf geometry polygon, but I have little idea how to achieve this goal.
Data looks like this:
a <- c("[30.523311, 50.40919], [30.523111, 50.409093], [30.522475, 50.408781], [30.522484, 50.408771], [30.523591, 50.407804], [30.524049, 50.407403], [30.526558, 50.406062], [30.526791, 50.405939], [30.527487, 50.4057], [30.527787, 50.405564], [30.528793, 50.405209], [30.528718, 50.404554], [30.530223, 50.404552], [30.530133, 50.404363], [30.529104, 50.404185], [30.529018, 50.403965], [30.528933, 50.403337], [30.529986, 50.403227], [30.531422, 50.403077], [30.531336, 50.402585], [30.531743, 50.402489], [30.531612, 50.401577], [30.531505, 50.401447], [30.531483, 50.401211], [30.531489, 50.40103], [30.531873, 50.400999], [30.531932, 50.400429], [30.531961, 50.400257], [30.531965, 50.400233], [30.532439, 50.400174], [30.533329, 50.400062], [30.533369, 50.399868], [30.533435, 50.399542], [30.533478, 50.39954], [30.534231, 50.399498], [30.534229, 50.399863], [30.5354583263, 50.4005476511], [30.5361664295, 50.4004929412], [30.5361825227, 50.4008211999], [30.5366814136, 50.4008656514], [30.537257, 50.400902], [30.537703, 50.400882], [30.538053, 50.400829], [30.538949, 50.40062], [30.539305, 50.400619], [30.539605, 50.40051], [30.540023, 50.40049], [30.54056, 50.400838], [30.540828, 50.400443], [30.541166, 50.400788], [30.541501, 50.401137], [30.542203, 50.40115], [30.54236, 50.40171], [30.542792, 50.401676], [30.543223, 50.401641], [30.548297, 50.400637], [30.558722, 50.398574], [30.558969, 50.398794], [30.559252, 50.399041], [30.559338, 50.399931], [30.55938, 50.400163], [30.559447, 50.400539], [30.559532, 50.400916], [30.559051, 50.400983], [30.555229, 50.401518], [30.555258, 50.402649], [30.554082, 50.402791], [30.553204, 50.402945], [30.552119, 50.40318], [30.551907, 50.403224], [30.551414, 50.403334], [30.55056, 50.403525], [30.550566, 50.403544], [30.547881, 50.404161], [30.547819, 50.404175], [30.546416, 50.404497], [30.544978, 50.404827], [30.54398, 50.404963], [30.543771, 50.404992], [30.54248, 50.405173], [30.540812, 50.405404], [30.538729, 50.405488], [30.537509, 50.405536], [30.536298, 50.405576], [30.534761, 50.405593], [30.53247, 50.405613], [30.530065, 50.40586], [30.528, 50.406381], [30.527158, 50.406595], [30.525696, 50.407249], [30.52562, 50.407313], [30.525481, 50.40743], [30.525423, 50.407478], [30.525274, 50.4076], [30.524885, 50.407919], [30.524221, 50.408445], [30.523308, 50.409165], [30.52333, 50.409175], [30.523311, 50.40919]")
b <- c("[30.517824, 50.405568], [30.517689, 50.40543], [30.517727, 50.405406], [30.517798, 50.405361], [30.517947, 50.405265], [30.518736, 50.405337], [30.519452, 50.405434], [30.520105, 50.405521], [30.52047, 50.405706], [30.520844, 50.405896], [30.521225, 50.405894], [30.521758, 50.405744], [30.524134, 50.404085], [30.524483, 50.403844], [30.524582, 50.403776], [30.524901, 50.403571], [30.524915, 50.403562], [30.524967, 50.403529], [30.525058, 50.40347], [30.525317, 50.403308], [30.525573, 50.403147], [30.526089, 50.402824], [30.526607, 50.4025], [30.527259, 50.402092], [30.527528, 50.401796], [30.528232, 50.40102], [30.528482, 50.400748], [30.528687, 50.400524], [30.528731, 50.400476], [30.528953, 50.400242], [30.529032, 50.400158], [30.529059, 50.400054], [30.529836, 50.399809], [30.530864, 50.399677], [30.530971, 50.39987], [30.53074, 50.400041], [30.53095, 50.400096], [30.53118, 50.401053], [30.531489, 50.40103], [30.531483, 50.401211], [30.531505, 50.401447], [30.531612, 50.401577], [30.531743, 50.402489], [30.531336, 50.402585], [30.531422, 50.403077], [30.529986, 50.403227], [30.528933, 50.403337], [30.529018, 50.403965], [30.529104, 50.404185], [30.530133, 50.404363], [30.530223, 50.404552], [30.528718, 50.404554], [30.528793, 50.405209], [30.527787, 50.405564], [30.527487, 50.4057], [30.526791, 50.405939], [30.526558, 50.406062], [30.524049, 50.407403], [30.523591, 50.407804], [30.522484, 50.408771], [30.519427, 50.407208], [30.519404, 50.407185], [30.519397, 50.407177], [30.518728, 50.406494], [30.518557, 50.406319], [30.517824, 50.405568]")
polygons<- as.data.frame(c(a,b), ncol=1, nrow=2)%>%
rename(polygon=1)
You need to parse these text strings into numbers, then convert into polygons. This function creates an sfc object that contains the polygons these vectors describe:
polygonise <- function(strings) {
do.call(c, lapply(strings, function(x) {
cutstring <- unlist(strsplit(x, "\\[|\\]"))
cutstring <- cutstring[nchar(cutstring) > 3]
sf::st_sfc(sf::st_polygon(list(do.call(rbind,
lapply(strsplit(cutstring, ", "), as.numeric)))))
}))
}
So, for example, we can do:
polygons$geometry <- polygonise(polygons$polygon)
ggplot(polygons) + geom_sf(aes(geometry = geometry), fill = "forestgreen")
Your solution works perfectly on the example I provided. However, when I try to use your function on real data I get an error ("'MtrxSet(x, dim, type = "POLYGON", needClosed = TRUE)': polygons not (all) closed ". Perhaps my coordinates are flawed? Sorry for that, but I’m pretty new to R.
a <- read_csv("https://raw.githubusercontent.com/slawomirmatuszak/Covid.UA/master/dzielnice.csv")
b<- polygonise(a)

The New York Times API with R

I'm trying to get articles' information using The New York Times API. The csv file I get doesn't reflect my filter query. For example, I restricted the source to 'The New York Times', but the file I got contains other sources also.
I would like to ask you why the filter query doesn't work.
Here's the code.
if (!require("jsonlite")) install.packages("jsonlite")
library(jsonlite)
api = "apikey"
nytime = function () {
url = paste('http://api.nytimes.com/svc/search/v2/articlesearch.json?',
'&fq=source:',("The New York Times"),'AND type_of_material:',("News"),
'AND persons:',("Trump, Donald J"),
'&begin_date=','20160522&end_date=','20161107&api-key=',api,sep="")
#get the total number of search results
initialsearch = fromJSON(url,flatten = T)
maxPages = round((initialsearch$response$meta$hits / 10)-1)
#try with the max page limit at 10
maxPages = ifelse(maxPages >= 10, 10, maxPages)
#creat a empty data frame
df = data.frame(id=as.numeric(),source=character(),type_of_material=character(),
web_url=character())
#save search results into data frame
for(i in 0:maxPages){
#get the search results of each page
nytSearch = fromJSON(paste0(url, "&page=", i), flatten = T)
temp = data.frame(id=1:nrow(nytSearch$response$docs),
source = nytSearch$response$docs$source,
type_of_material = nytSearch$response$docs$type_of_material,
web_url=nytSearch$response$docs$web_url)
df=rbind(df,temp)
Sys.sleep(5) #sleep for 5 second
}
return(df)
}
dt = nytime()
write.csv(dt, "trump.csv")
Here's the csv file I got.
It seems you need to put the () inside the quotes, not outside. Like this:
url = paste('http://api.nytimes.com/svc/search/v2/articlesearch.json?',
'&fq=source:',"(The New York Times)",'AND type_of_material:',"(News)",
'AND persons:',"(Trump, Donald J)",
'&begin_date=','20160522&end_date=','20161107&api-key=',api,sep="")
https://developer.nytimes.com/docs/articlesearch-product/1/overview

Python ArcPy - Print Layer with highest field value

I have some python code that goes through layers in my ArcGIS project and prints out the layer names and their corresponding highest value within the field "SUM_USER_VisitCount".
Output Picture
What I want the code to do is only print out the layer name and SUM_USER_VisitCount field value for the one layer with the absolute highest value.
Desired Output
I have been unable to figure out how to achieve this and can't find anything online either. Can someone help me achieve my desired output?
Sorry if the code layout is a little weird. It got messed up when I pasted it into the "code sample"
Here is my code:
import arcpy
import datetime
from datetime import timedelta
import time
#Document Start Time in-order to calculate Run Time
time1 = time.clock()
#assign project and map frame
p =
arcpy.mp.ArcGISProject(r'E:\arcGIS_Shared\Python\CumulativeHeatMaps.aprx')
m = p.listMaps('Map')[0]
Markets = [3000]
### Centers to loop through
CA_Centers = ['Castro', 'ColeValley', 'Excelsior', 'GlenPark',
'LowerPacificHeights', 'Marina', 'NorthBeach', 'RedwoodCity', 'SanBruno',
'DalyCity']
for Market in Markets:
print(Market)
for CA_Center in CA_Centers:
Layers =
m.listLayers("CumulativeSumWithin{0}_{1}_Jun2018".format(Market,CA_Center))
fields = ['SUM_USER_VisitCount']
for Layer in Layers:
print(Layer)
sqlClause = (None, 'ORDER BY ' + 'SUM_USER_VisitCount') # + 'DESC'
with arcpy.da.SearchCursor(in_table = Layer, field_names = fields,
sql_clause = sqlClause) as searchCursor:
print (max(searchCursor))
You can create a dictonary that stores the results from each query and then print out the highest one at the end.
results_dict = {}
for Market in Markets:
print(Market)
for CA_Center in CA_Centers:
Layers =
m.listLayers("CumulativeSumWithin{0}_{1}_Jun2018".format(Market,CA_Center))
fields = ['SUM_USER_VisitCount']
for Layer in Layers:
print(Layer)
sqlClause = (None, 'ORDER BY ' + 'SUM_USER_VisitCount') # + 'DESC'
with arcpy.da.SearchCursor(in_table = Layer, field_names = fields,
sql_clause = sqlClause) as searchCursor:
print (max(searchCursor))
results_dict[Layer] = max(searchCursor)
# get key for dictionary item with the highest value
highest_count_layer = max(results_dict, key=results_dict.get)
print(highest_count_layer)
print(results_dict[highest_count_layer])

R: Plot ARC/INFO Generate File

I have an ARC/INFO generate file whose contents look like:
3594 -124.049541 44.429077
-123.381222 44.530192
-123.479913 44.625517
-123.578917 44.720704
-123.678234 44.815755
-123.777866 44.910669
-123.946044 44.885032
-124.114074 44.858987
-124.281949 44.832529
-124.449663 44.805654
-124.516511 44.684660
-124.583091 44.563597
-124.649404 44.442465
-124.715451 44.321261
-124.615376 44.227772
-124.515601 44.134147
-124.416125 44.040385
-124.316948 43.946486
-124.151513 43.973082
-123.985926 43.999247
-123.820193 44.024987
-123.654322 44.050307
-123.586447 44.170362
-123.518307 44.290360
-123.449899 44.410303
-123.381222 44.530192
END
3595 -123.103772 45.009223
-122.427717 45.101578
-122.525757 45.198252
-122.624122 45.294789
-122.722814 45.391191
-122.821833 45.487459
-122.992014 45.464007
-123.162072 45.440175
-123.332002 45.415959
-123.501798 45.391355
-123.571234 45.271264
-123.640389 45.151121
-123.709266 45.030923
-123.777866 44.910669
-123.678234 44.815755
-123.578917 44.720704
-123.479913 44.625517
-123.381222 44.530192
-123.213811 44.554460
-123.046278 44.578334
-122.878629 44.601816
-122.710869 44.624913
-122.640504 44.744148
-122.569859 44.863337
-122.498931 44.982480
-122.427717 45.101578
END
3676 -122.989567 44.147495
-122.323040 44.238368
-122.419523 44.335217
-122.516322 44.431923
-122.613437 44.528488
-122.710869 44.624913
-122.878629 44.601816
-123.046278 44.578334
-123.213811 44.554460
-123.381222 44.530192
-123.449899 44.410303
-123.518307 44.290360
-123.586447 44.170362
-123.654322 44.050307
-123.556277 43.955264
-123.458534 43.860080
-123.361093 43.764751
-123.263953 43.669279
-123.098838 43.693189
-122.933613 43.716694
-122.768285 43.739802
-122.602857 43.762515
-122.533309 43.881546
-122.463492 44.000532
-122.393403 44.119472
-122.323040 44.238368
END
END
My strategy is to read in the file generating a list of latitude-longitude points and beginning a new unique group id every time I encounter an END. I'll then plot using ggplot" andgeom_polygon".
Alas, I'm not sure how to efficiently accomplish the reading of the file.
Any thoughts?
Read the spatial task view on CRAN and then use readOGR from the rgdal package to read into an sp class object. You'll need a GDAL/OGR install with ARCGEN format support, which despite being listed as 'compiled by default' Link I don't have on my system.
Failing that, open the file as a connection, read each line, build a Polygon, then Polygons and SpatialPolygons.
Here's a fairly sub-optimal but working function:
readUng <- function(f){
require(sp)
stream = file(f,"r")
first = readLines(stream,1)
bits = strsplit(first," ")[[1]]
polys = list();ids=NULL
while(TRUE){
id=bits[1] # label pt = bits[2],bits[3]
ids=c(ids,id)
coords=NULL
while(TRUE){
xy=readLines(stream,1)
if(xy=="END"){
break
}
coords=rbind(coords,strsplit(xy," ")[[1]])
}
polys[[length(polys)+1]] = Polygons(list(Polygon(matrix(as.numeric(coords[,2:3]),ncol=2))),ID=id)
lines = readLines(stream,1)
if(lines == "END"){
break
}
bits = strsplit(lines," ")[[1]]
}
return(SpatialPolygons(polys))
}
Now its a proper spatial data object, you can also give it a coordinate system (looks like lat-long to me, so epsg:4326, but only you know). Now you could modify all this to produce whatever ggplot wants, but if its spatial data then you should keep it as a spatial data class and ggplot should be made capable of dealing with such.

Resources