Is there any code for get original Distance travelled in my car via OBDII in case of DTC Code clean-up - obd-ii

now at i use o1 31 PID to get Distance travelled but this give only Distance traveled since codes cleared-up not total travelled KM

Unfortunately OBD2 does not specify any standard PIDs to get this information. That said, you might be able to gather this via vendor-specific extensions or by dropping down to a lower level (using direct CAN commands), but this is not portable between vehicles and thus discouraged.

Related

MySQL scaling geolocation query

So I have thousands of users with latitude and longitude. They check in with new coordinates every 30 seconds.
When they check in I need to send them the 100 people closest to them no matter how far away they are. In a crowded city this may be a radius of half mile. In the country it could take a radius of 100 miles to get 100 people.
It's easy enough to calculate the distance of each user from the user checking in and then do LIMIT 100. But that essentially does a table scan, calculates the distance between the checking in user and all other users in the table, sorts them by distance and then takes 100.
Won't be efficient at scale.
So what strategy can I use to scope the query to a subset of users and still get 100 results?
I don't think MySQL will be helpful for a longer duration. I'd recommend checking out the SingleStore database for your use case since it's efficient, scalable, and faster.
For your reference, Please go through the documentation by clicking the link here.

How to optimize my pathfinding algorithm on a GTFS network

Intro
I'm using a slightly modified GTFS database.
I have a first step algorithm that given two geographical locations provides:
the list of stops around departure and arrival
the list of routes that connects those list of stops
The second step algorithm finds the best journeys matching those stops and routes.
This is working well on direct journeys as well as journeys using one connection.
My problem arises when trying to find the best journey using 2 connections (so there are 3 trips to be searched).
Database
The GTFS format has the following tables (each table has a foreign key to the previous/next table in this list):
stops: stop information (geolocation, name, etc)
stop_times: timetable
trips: itinerary taken by a vehicle (bus, metro, etc)
routes: family of trips that roughly take the same path (e.g. standard and express trips on the same route, but different stops taken)
I have added the following tables
stop_connections: stop to stop connections (around 1 to 20)
stops_routes: lists the available routes at every stop
Here's the table row count in a city where I get slow results (Paris, France):
stops: 28k
stop_times: 12M
trips: 513k
routes: 1k
stop_connections: 365k
stops_routes: 227k
Algorithm
The first step of my algo takes two latitude/longitude points as input, and provides:
the list of stops at each location
the routes that can be used to connect those stops (with up to two connections)
The second step takes each start stop, and analyses the available journeys that use only the routes selected by the first step.
This is the part that I'm trying to optimize. Here's how I'm querying the database:
My search terms (green in the picture):
one departure stop
several arrival stops (1 to 20)
allowed routes at departure, at first connection and on last trip
service ID (not relevant here, can be ignored)
Here's what I do now:
Start from a stop => get timetable => get trips => get routes; filter on allowed routes.
Connect the arrival stops of the first trip to a list of possible stops using stop_connections
Repeat from step 1 two times so that I have 3 trips/2 connections
The problem
This is working fine on some cases, but it can be very slow in others. Usually as soon as I join the timetable or the stop connections, there is a 10x increase of the returned rows. Since I'm joining these table 8 times, there are potentially 10^8 rows to be searched by the engine.
Now I'm sure that I can get this to be more efficient.
My problem is that the number of rows increases at every join, and the arrival stop selection is made at the very end.
I mean I get all the possible journeys from a given stop at a given departure time (there can be millions of combinations), and only when my search reaches the last trip, I can filter on the ~20 allowed arrival stops.
It could be much faster if I could somehow 'know' soon enough that a route isn't worth searching.
Optimizations
Here's what I tried/thought of:
1. Inner join stops_routes when joining stop_connections
Only select stops at a connection that lead to the allowed routes at next trip.
This is sometimes efficient when there is a lot of connections and not all the connected stops are interesting (some connected stop might only be used by a route we don't want to take).
However this inner join can increase the number of rows if there are not many connected stops and a lot of allowed routes.
2. Partition the stop_times table
Create a smaller copy of the stop_times that contains only the timetable of the next two hours or so. Indeed, having the database engine search for the timetable (up to 10pm for example) when my trips starts at 8am is useless. Keeping only 8am-10am is enough and much faster.
This is very efficient, because it dramatically decreases the number of rows to be searched.
I have implemented this with success, it decreased the search time by a factor of about 10x or even 100x.
3. Identify 'good' and 'bad' routes
There is usually, in a metropolitan area, large routes that are very useful when travelling large distances. But these routes aren't the best option when travelling small distances. A human person who knows his own city's public transportation system will quickly tell that from this neighborhood to this other, the best option is to take a specific route.
However this is very difficult to do, and requires a customization on every city.
I plan to make this algo completely independant of the city, so I'm not really willing to go down that road
4. Use crowdsourcing to identify paths that work well
The first search is slow, but the information taken from it can be used to serve fast result to the next person with a similar journey.
However there are so many combinations of departure and arrival stops that the information taken from one query might not be very useful.
I don't know if this is a good idea. I haven't implemented it.
Next
I'm running out of ideas. I know this is not a programming question, but rather a request of ideas on an algorithm. I hope this falls into the SO scope.
Having it on a network makes things a little bit interesting, but fundamentally, you're doing pathfinding, which is a slow process. You're running into the exponential nature of the problem, and doing so with only 3 connections.
I have a couple suggestions that you can perhaps use while doing this with mysql, and a couple that are likely not implementable within it.
Rather than partitioning the timetable, only take the next time for any given route. If you're leaving at 8 AM, you're correct, only looking at routes from 8-10 is better than looking at them all. However, if there's a route from A-B that leaves at 8:20, 8:40, 9:00, 9:15, 9:25, 9:45... there is zero reason to take them all: just take the first arrival time for any given route, since it's strictly better than the rest.
I presume you are pruning any routes that return to an already-visited location? If not, you perhaps should be: they're not useful for you. This may be somewhat difficult to do within the SQL framework.
Depending on its coverage, you could perhaps find a path using the (much smaller) routes table, and then find the best implementation of the top working paths from the trips table.
This is likely impossible within the framework of SQL, but the thing that makes most decent pathfinding algorithms fast is that they use a heuristic to search. Your search goes down every possible route -- it would be a lot faster to first look down the route that leads in the right direction. If it doesn't pan out, less likely directions are picked. The key here is that as soon as you have a result, you return it -- you effectively pruned every route you didn't yet search by the time you returned an answer.
Pre-calculated preferred routes: you suggest this would require human intervention, but I counter that you could do it computationally. Spend the time properly searching for routes from various points to various other points, and check on the statistics of how the routes worked. I would expect that you will find things allowing you to make a "anywhere over here to anywhere over there is going to use this intermediate path" table -- your problem is reduced from "find a path from A to B" to "find a path from A to C, followed by a path from D to B". Doing this will have the potential of causing you to find sub-optimal routes (as you are making an assumption from the precalculated statistics), but it may let you find that sub-optimal route much faster. On a mesh layout it will not work at all well; on a hub layout it will work excellently.
Thanks to zebediah49, I have implemented the following algorithm:
0. Lookup tables
First, I have created an ID on the trips table, that uniquely identifies it. It is based on the list of stops taken in sequence. So this ID guarantees that two trips with the same ID will take exactly the same route.
I called this ID trip_type.
I have improved my stop_connections table so that it includes a cost. This is used to select the best connection when two 'from' stops are connected to the same 'to' stop.
1. Get trips running from the departure stop(s)
Limit those trips to only 1 per trip type (group by trip_type)
2. Get arrival stops from these trips
Select only the best trip if there are two trips reaching the same stop
3. Get connected stops from these arrival stops
Select only the best connection if there are >1 stops that are connected to the same stop
4. Repeat from step 1
I have splitted this into several subqueries and temporary tables, because I can easily group and filter the best stops/trips at each step. This ensures that the minimum searches are sent to the SQL server.
I have stored this algorithm into an SQL procedure, that will do this in a single SQL statement:
call Get2CJourneys(dt, sd, sa, r1, r2, r3)
Where:
dt: departure time
sd: stops at departure point
sa: stops at arrival point
r1, r2, r3: allowed routes for the 1st, 2nd and 3rd trips
The procedure call returns interesting results in <600ms where my previous algorithm returned the same results in several minutes.
Expanding on #zebedia49's fourth point, you can precompute the vector traveled by a route, e.g. a route going due north has a vector of 0, due west = 90, due south = 180, due east = 270. Only return routes whose vectors are within, say, +/- 15 modulo 360 degrees from the as-the-crow-flies route (or +/- 30 if the +/- 15 query doesn't return any hits).

Sort by distance, how do you think I should do this?

My Company currently runs a listing service of family activities. In our CMS we have two types of entities Branches (The shops we list) and Events (Special Offers, Occasions etc).
Typically when listing an event we would say which Branches it is for and create a relationship, we would search the near by shops for events. Grab them and sort them by distance.
Now our clients want to be able to list a one off event that hasn't got a branch associated with it (For example they host a Festival at a near by garden center rather than one of their shops), I can easily make it I can sort these by distance as well.
But what I was wondering is how could combine the both, so one of our apps could go to our API, "Dude, where are 10 events near to whee I am right now ? " and the api would pull up a list of the 10 closest events.
It should be able to handle Events that are using the location of Branches as well as having its own unique location.
Or do you think I should just store location as its own entity or have hidden branches, places we can set up as being where the event is happening but don't actually show up as being a branch in the app :)
If you have lat / long positions for your events and your branches you can apply the Haversine Formula to compute approximate distances, then order by ascending distance.
MySQL can do this, if you're willing to use a hairy query. This note from the Google Maps team gives the query. You don't have to use Google Maps to do this; you just need lat/long information for each place involved.
https://developers.google.com/maps/articles/phpsqlsearch_v3
Edit It's true that this is very slow if you compute the distance between many pairs of places. The trick to making this kind of operation fast is using a bounding box (spherectangular) distance limit, and putting indexes on your latitude and longitude.
Look at this: Geolocation distance SQL from a cities table
MYSQL has support for "spacial databases" as the spacial extension This will allow you to use "spacial" datatypes in your columns, as well as build index on them, and perform various "spacial analysis" such as polygon intersection.
Not sure this is what you need, but that may worth investigations.

How do I make a simple bus route search Engine?

[Not:e user is asking this again at Development of railway enquiry system, how to model Trains, Stations and Stops? ]
My Problem Description:
Suppose I have a BUS-123 in ROUTE-1 it will travel through A, B, C, D, E, F, G, H and BUS-321 in ROUTE-2 through D, E, F, X, Y, Z .
if someone enters B as a source point and F as a destination point then ROUTE-1 with BUS-123 should display in the result. But if someone enters H as a source and A as destination result should not display, because returning may not always same with one that is traveled.
But if a person enters A as a source and Z as destination then BUS-123 with ROUTE-1 and BUS-321 with ROUTE-2 should display.
My Problem is:
How do I store that route information in Database? if i store in RDBMS like the following
BUS_NUMBER ROUTE_NUMBER VIA_ROUTES
BUS-123 ROUTE-1 A, B, C, D, E, F, G, H
BUS-321 ROUTE-2 D, E, F, X, Y, Z
Then how my search will work. I mean how to search it in a string.
And if I store all the VIA_ROUTES in different different columns then how it will be..? Please suggest me with your own technique. It is not urgent but I am planning to make a basic bus route search, so your comment with help is appreciated.
I'd model it as a cyclic graph. Each bus stop is represented by a vertice. Each direct connection between two stops is represented by an edge labelled with the route number; consequently, each route is a sequence of connected edges. Make the edges directed, too. Not all routes travelling from stop A to stop B will necessarily also travel from stop B to stop A in the other direction.
Probably want to populate each edge with the estimated travel time, a measure (or measures) of variance for that leg -- at 2am on a Sunday night, the variance might be low, but at 5pm on a Friday evening, it might be very high, and list of departure times as well.
Then its a matter of graph traversal and finding the "least cost" route, however you choose to define "least cost" -- Factors you might want to consider would include:
Total travel time
Total time spent waiting for the next leg to depart.
Wait time at any individual stop.
Distance?
One should note that too much wait time is bad (ever spend 40 minutes waiting for a bus in January when it's -10 F?). Too little is bad, too, as it increases the probability of missing a connection, given that buses tend to have a fairly large variability to their schedules since they are highly responsive to fluctuations in local traffic conditions.
That's how I would do it.
I don't believe I'd try to solve it directly in SQL, though.
The model is a good fit for SQL, though. You need the following entities, and then some, since you'll need to represent schedules, etc.:
Stop. A Bus stop. The vertices of the graph.
Route. A bus route.
Segment. The direct link between two stops. The edges of the graph.
RouteSegment. An associative entity representing ordered sequence of segments that composes the route.
I think the bus_numbers aren't important because you can look them up later. Maybe what you need is to create a 2d matrix with the bus_stops in a big matrix having them all and then use a graph traversing algorithm like dijkstra to find the shortest path from A to B. When you got that you can easily lookup the bus_numbers and show them to the client. Thus I think your database is already very good.
I'd have a route table and a route_part table. The latter would contain a reference to the route, plus an ordinal number for sorting, and a reference to a stop table. Thus, you can store any route.
In terms of searching, if you wish to search for a route between two stops, you could look up the two stops in the route_part table and see if they appear on the same route in any cases (bearing in mind that a route may exist in one direction and not the other).

How can we calculate sea distances using waypoints?

I have one query in which I really stuck at that. I have port database with waypoints and also routing points which I need to use in distance calculation between two ports. I have done lots of R&D to find formula which gives me distance between two points. I also need shortest route which is possible.
I have reviewed online tools which allow user to calculate the distance. But I want to do the same at my own. I have reviewed Port World Distance Calculator. I reviewed the Great Circle formula to achieve that but I don't know that how we avoid landscapes in sea distance and in which direction I need to find distance for second port.
The simpliest that comes - just create a table in your database like:
port1id : port2id : distance
number of ports are limited, and this table will give you flexibility to set correct distance