skip to Main Content

I’m working on a module for a transport app.

My boss gave me this task: We have a class called CitizenRequest where there’s an attribute, called PointPath, which stores the latitude and longitude of a citizen’s location.

This data is stored in a JSON file in a MongoDB database.

What I have to do is: Take this JSON, find the minimum PointPath (or else, minimum latitude and longitude), the maximum, and use this to positions to make a square, which will represent our area of transport.

Now, I have to divide this square into other mini squares, and each one of these will become a "zone". This zone will then have an ID, and must be stored into another collection.

The purpose of this collection should be to store how many requests we have for each zone. So, for example, we will see that in the zone with ID=149, we have X requests.

To do so, I should create an algorithm that analyzes latitude and longitude to understand in which zone it falls.

I know this may sound pretty difficult. Still, I have absolutely no idea how to implement this. So I’m asking you if you have solutions or ideas. I don’t really think I must use APIs to do so, I think it just requires some logic behind it. Any help?

2

Answers


  1. To rephrase,so I understand correctly.
    You have one big square which is your valid area (area of transport) and in this square you have a grid, where you try to see where the most requests come from.

    First lattitude and longlitude values Look confusing and nothing saying at first: 10,000001 20,002567.
    Change that, make it human readable.

    If I unverstanden correctly, you need to start and find your point 0.

    If you Think of your valid area as a square it would be the bottom left most point. If it is 5.0002345, 10.124567 you would subtract that value of all other values. 5.0003345 10.124567 => 0.0001, 0 so a little bit heigher, than your startingpoint.

    Now its way easier to find all the requests in your valid zone. You would take all the points and plot them in an easy x/y plot. Well the implementation I dont know about your dev setup.

    I would recomend an simple KNN algorhytmus, to Cluster the request. Maybee the plot itself will be sufficent.

    Login or Signup to reply.
  2. Assume docs contain points like this:

    {"citizenID:"C2", loc: { type: "Point", coordinates: [ -76.738988, 39.960921 ] }}
    

    then something like this will capture locations in squares inside the bounding rectangle:

    // First, scan the whole collection to get topleft and bottomright max points:
    c=db.foo.aggregate([
        {$group: {_id:null,
                  L: {$min: {$first: '$loc.coordinates'}},
                  R: {$max: {$first: '$loc.coordinates'}},
                  T: {$max: {$last: '$loc.coordinates'}},
                  B: {$min: {$last: '$loc.coordinates'}}
                 }}
    ]);
    d = c.next();
    
    var leftLon = d['L'];
    var topLat = d['T'];
    var rightLon = d['R'];
    var bottomLat = d['B'];
    
    var incr = 0.01;  // this is size of square.  0.01 is 1km.                                       
    var totSqr = 0;
    
    // Left to right, top to bottom:                                                   
    for(var lon = leftLon; lon < rightLon; lon += incr) {
        for(var lat = topLat; lat > bottomLat; lat -= incr) {
    
            // Make a square:                                                          
            var coords = [];
            coords.push( [ lon, lat ] );
            coords.push( [ lon+incr, lat ] );
            coords.push( [ lon+incr, lat-incr ] );
            coords.push( [ lon, lat-incr ] );
            coords.push( [ lon, lat ] ); // must close loop!                                 
    
            c = db.foo.aggregate([
                {$match: { "loc": { $geoWithin: { $geometry:
                                  { type: "Polygon", coordinates: [ coords ] } }}
                         }}
                ,{$group: {_id:null, n: {$sum:1}}}
            ]);
            d = c.next();
            if(d != null) {
                print("square region " + lon + "," + lat + ": found " + d['n']);
                // optionally insert the data somewhere as the OP notes.  You can
                // create the zone ID with an incrementing number e.g.
                // db.results.insertOne({"zone": "Z"+(zid++), n: d['n']});
            }
            totSqr++;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search