Calculate distance using googleway - r

I am using googleway package in R to calculate distances between two points. I had 2 variables names household latitude and longitude and 2 other variables containing lat and long of the corresponding government cooperative store in the locality of the household. So one can easily specify the origin and destination coordinates.
But now i want to calculate the distance of a household from all the household in its locality. There are 12 localities in total, and in each locality there are about 50 (with some variation) households. So for each locality, i need to calculate 49 distances(excluding itself). Can someone help me on how to do it?

Related

Show regional cluster using python - folium and other vizualization library

I'm working on a dataset of location characteristics that I separate into 7 clusters.
I have 7 characteristic clusters with, in each, between 20 and 2000 locations.
Each cluster have a score (between 0 and 1).
My goal is to do a regional clusterizing on each characteristic cluster, then to show these regional clusters on a map with a color based on the score on the characteristic cluster.
Example : I have 1000 locations. Each location have a temperature. I detect 3 clusters within the data.
I separate these 1000 locations into 3 clusters:
1st_cluster : 500 locations and a score of (=mean of temperature of all locations) 0 degrees
2nd_cluster : 300 locations and a score of -10 degrees
3rd_cluster : 200 locations and a score of +10 degrees
That part is already done.
I want to show on a map these 3 clusters BUT to detect regional tendancy.
For now, I used folium to plot each characteristic (=temperature) cluster individually and let the library detect regional behavior.
But I couldn't find a way to plot on the same map all clusters (my code only detect when multiple locations are in the same area and create a regional cluster).
I don't really know if folium is a good library to continue and, if not, I don't know what library to use and how.
Do you have any ideas and advices ?
Thanks

How to calculate a sum of an area-weighted polygons within a polygon in R?

I have blocks of census data (shapefile with the column of interest being pop20) and polygons of areas of interest (shapefile with the column of interest being site). I am trying to get a sum of the population within each of the areas of interest (see example of one area of interest and the census blocks below). I don't know how to join the population estimates (column: pop20) to the areas of interest and account for polygons that are only partially within the areas of interest.
Hence I am interested in the following:
what is the population within each census block within each area of interest, accounting for some blocks only being partial inside (so if 1/2 the block is within the area of interest, assume the population is 1/2 of the value in pop20).
Then what is the sum of all the blocks within the area of interest weighing the blocks that are only partially within the area of interest from part 1.
I have essentially imported by shapefiles using the sf package but then I don't know what to do (do I use st_intersection or st_join or something else)?
pop<-st_read("...\\pop_census2020_prj.shp")
buff<-st_read("...\\trap_mcpbuff_prj.shp")
Thank you for your help.

Normalizing species count data in ArcGIS Pro

I have presence points of a certain species all over the United States. I completed a spatial join between the US and said points. However, I am unsure of how to normalize the data. There is a "percent of total," but I am unsure if this is the appropriate option. Or is it as simple as just normalizing by the counts themselves?
It depends on what comparison you're trying to make with the normalized data.
If you want to look at the occurrence of that species by state, you could do a spatial join on a US States layer, then calculate a new field where the value is the species count for each state divided by the total area of the state. That would give you the normalized 'count per square mile' (or whatever unit you want).

Number of geocoded points in concentric circles in R

I have a data set of around 36K hotels geocoded with latitude and longitude.
For each of them, I would need to know how many other hotels (and also which of the others) are placed in different concentric circles around each point ( 2miles, 5miles, 10miles).
For example, the dataset looks like this:
ID Latitude Longitude Rooms
1 N K 200
2 N K 150
3 N K 80
4 N K 140
5 N K 100
I would need a measure of density for each hotel in each concentric circle (which is normally calculated by dividing the number of room of the focal hotel per hotel by the total number of rooms in its concentric circle)
Normally, I would calculate the distance between each point and then filter for the ones that are within each distance but with 36k points, it would take a lot of time because I would go to calculate the distance among each point when I probably need the distance for each point with other 4-5 others maximum.
Do you have an idea on how to calculate the distance and then the density efficiently using R or ArcGIS?
Thanks
It seems the best way to make your code more efficient is not by getting a more efficient distance calculating algorithm, but by only applying that algorithm to a couple of hotels.
You could do a rough "square" approximation very quickly:
make a new dataset of hotels sorted by latitude
make a new dataset of hotels sorted by longitude
For each hotel:
make 2 new empty lists: hotels_in_lat_range and hotels_in_long_range
start at your hotel in the latitude-sorted dataset, and go up until you reach a certain limit
go back down until you reach a lower limit, adding the hotels to hotels_in_lat_range as you go along
repeat steps 4 and 5 for the longitude-sorted dataset, adding hotels to hotels_in_long_range
for every hotel that is in both lists, calculate the distance between your test hotel and that hotel. If the distance is less than your circle radius, include it when you calculate the density.
For the upper and lower limits of latitude and longitude, I'd recommend using the following approximation (I wrote this in Python because I don't know R):
min_lat = max(-89.9, test_lat - 4 * math.degrees(test_rad/Earth_rad))
max_lat = min(89.9, test_lat + 4 * math.degrees(test_rad/Earth_rad))
min_long = max(
-180.0,
test_lat - 4 * math.degrees(
test_rad/(Earth_rad * min(cos(min_lat), cos(max_lat)))
)
)
max_long = min(
180.0,
test_lat + 4 * math.degrees(
test_rad/(Earth_rad * min(cos(min_lat), cos(max_lat)))
)
)
This is a reasonable approximation when your testing radius is significantly smaller than the Earth's radius. I'd recommend staying within 100 miles.

Get all cities in latitude/longitude range

I have a list of cities, each of them obviously has a longitude and latitude.
Now selecting one of these cities, i want to obtain all the other cities that have a longitude / latitude in a range of 50 km from the selected city.
What formula should I use?
I am only interested in the mathematical formula to convert km to latidutine and longitude from a know city position
Then i will calculate the maximum and minimum latitude and longitude, for considering an acceptable range. (like a Square)
tks
I don't want to calculate the distance between two points!+
I want to calculate min e max latitude and longitude and then filter my cities by this coordinates.
I've found a sample in Php that worked for me.
(i've ported it to C#)
http://blog.fedecarg.com/2009/02/08/geo-proximity-search-the-haversine-equation/
You'd probably want to use the Haversine formula.
You may want to check out the following articles for further reading and for a few implementations in various languages:
Calculate distance, bearing and more between Latitude/Longitude points by Chris Veness.
Calculate Distance Between Two Points on a Globe in 11 languages.
You are looking for the Great Circle calculation. It returns the shortest distance across the surface of the planet given two points and their respective latitude and longitude. There is a great Wikipedia article here:
http://en.wikipedia.org/wiki/Great-circle_distance
There is a pretty decent implementation - source code in javascript here:
http://trac.osgeo.org/openlayers/wiki/GreatCircleAlgorithms

Resources