Taxi datset - lat lon colocation
4 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I’m practicing data science with NY Taxi data (https://www.kaggle.com/competitions/nyc-taxi-trip-duration/data). I want to check if certain taxis were collocated within a short distance from each other.
The task I’m trying to accomplish is:
- Measure the distance for each taxi (against all other taxis)
- If the taxis were within 20 m of each other – just an example
- Check the date column associate with those lat lon matches
- If the date match, meaning the two taxis visited the general area on the same date
- Return the two unique IDs of those taxis, the date, the distance, and the lat lon of both of them
Here is my progress and I would really appreciate your help.
taxi = readtable('yellow_tripdata_2015-01.csv') % reading the data
taxi.uniqueID = randi([1000000,9999999],[height(taxi),1]) % Creating random hopefully unique ID :)
% measuring the distance from pick up to drop off
taxi.distance_travelled_in_M = deg2km(distance(taxi.pickup_latitude, taxi.pickup_longitude, taxi.dropoff_latitude, taxi.dropoff_longitude))
% spliting date and time in two diffrent columns
taxi.date = datestr(datetime(taxi.tpep_pickup_datetime),'dd/mm/yyyy')
taxi.time = datestr(datetime(taxi.tpep_pickup_datetime),'HH:MM:SS')
% In this loop I took each lat lon point and ran it agaist the rest of the
% data- probably not an efficient way - open to suggestions
% then converting the distance to meters
% checking if the distance within for example 20 meters
% and the date is the same - this step is not implemented yet , need help
% return the unique ids of both taxis, date of that event, the location
% (lat,lon), the distacne how far from each other
for idx = 1:length(taxi.pickup_latitude)
my_dist = distance(taxi.pickup_latitude(idx), taxi.pickup_longitude(idx), taxi.pickup_latitude, taxi.pickup_longitude); % distqances in degrees
my_dist = deg2km(my_dist); % distance in meters
if my_dist < 2
pos_results = [pos_results;my_dist;taxi(idx:idx, ["uniqueID", "date", "pickup_latitude", "pickup_longitude"])]
end
end
2 个评论
Walter Roberson
2022-11-12
my_dist = distance(taxi.pickup_latitude(idx), taxi.pickup_longitude(idx), taxi.pickup_latitude, taxi.pickup_longitude); % distqances in degrees
Not really degrees. You are doing Euclidean distance calculations on a non-linear surface. The result is only meaningful if the data is recorded near the equator. New York City is 40.7128N and cosd() of that is about 0.76 so degrees longitude there are only roughly 3/4 of the distance of a degree latitude.
Have you considered calculating Great Circle Distance ?
回答(1 个)
Walter Roberson
2022-11-12
编辑:Walter Roberson
2022-11-12
Either do a custom distance calculation of Great Circle Distance, or else divide the longitudes by cosd() of the latitudes in order to adjust the two of them to be on the same scale, and then use Euclidean.
3 个评论
Walter Roberson
2022-11-12
https://www.mathworks.com/help/map/ref/distance.html if you have the mapping toolbox
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Earth, Ocean, and Atmospheric Sciences 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!