Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import packaging codes geocoordinates to MongoDB #731

Closed
hangy opened this issue Feb 12, 2017 · 1 comment
Closed

Import packaging codes geocoordinates to MongoDB #731

hangy opened this issue Feb 12, 2017 · 1 comment
Labels
MongoDB We have 2 mongodb collections: one for current products, and one for obsolete products 📍🏭 Packager codes https://blog.openfoodfacts.org/en/news/discover-what-food-products-are-made-near-you-with-made-near-

Comments

@hangy
Copy link
Member

hangy commented Feb 12, 2017

In order to implementing MadeNearMe efficiently (related to #712), it would be useful to import the packaging codes into MongoDB to make them searchable with a geospatial index. ProductOpener::Display::search_and_map_products has the product to get all coordinates.

@hangy hangy added 📍🏭 Packager codes https://blog.openfoodfacts.org/en/news/discover-what-food-products-are-made-near-you-with-made-near- MongoDB We have 2 mongodb collections: one for current products, and one for obsolete products labels Feb 12, 2017
@hangy hangy changed the title Normalize packaging codes geocoordinates Import packaging codes geocoordinates to MongoDB Feb 12, 2017
@hangy
Copy link
Member Author

hangy commented Feb 12, 2017

With the codes imported, we can run something like this:

db.products.aggregate([
  { $unwind: "$emb_codes_tags" },
  { $lookup:
    { from: "emb_codes",
      localField: "emb_codes_tags",
      foreignField: "emb_code",
      "as": "emb_codes_coordinates"
    }
  },
  { $unwind: "$emb_codes_coordinates" },
    { $match: {
     "emb_codes_coordinates.loc": {
       $geoWithin: {
          $geometry: {"type":"MultiPolygon","coordinates":[[[[-5.661949,54.554603],[-6.197885,53.867565],[-6.95373,54.073702],[-7.572168,54.059956],[-7.366031,54.595841],[-7.572168,55.131622],[-6.733847,55.17286],[-5.661949,54.554603]]],[[[-3.005005,58.635],[-4.073828,57.553025],[-3.055002,57.690019],[-1.959281,57.6848],[-2.219988,56.870017],[-3.119003,55.973793],[-2.085009,55.909998],[-2.005676,55.804903],[-1.114991,54.624986],[-0.430485,54.464376],[0.184981,53.325014],[0.469977,52.929999],[1.681531,52.73952],[1.559988,52.099998],[1.050562,51.806761],[1.449865,51.289428],[0.550334,50.765739],[-0.787517,50.774989],[-2.489998,50.500019],[-2.956274,50.69688],[-3.617448,50.228356],[-4.542508,50.341837],[-5.245023,49.96],[-5.776567,50.159678],[-4.30999,51.210001],[-3.414851,51.426009],[-3.422719,51.426848],[-4.984367,51.593466],[-5.267296,51.9914],[-4.222347,52.301356],[-4.770013,52.840005],[-4.579999,53.495004],[-3.093831,53.404547],[-3.09208,53.404441],[-2.945009,53.985],[-3.614701,54.600937],[-3.630005,54.615013],[-4.844169,54.790971],[-5.082527,55.061601],[-4.719112,55.508473],[-5.047981,55.783986],[-5.586398,55.311146],[-5.644999,56.275015],[-6.149981,56.78501],[-5.786825,57.818848],[-5.009999,58.630013],[-4.211495,58.550845],[-3.005005,58.635]]]]}
       }
     }
   }
  },
  { $group:
    {  _id:
      { emb_code: "$emb_codes_coordinates.emb_code" },
        count: { $sum: 1 }
      }
  }
]);

Which returns this on my test server:

{ "_id" : { "emb_code" : "uk-1005-ec" }, "count" : 1 }
{ "_id" : { "emb_code" : "uk-bb011-ec" }, "count" : 1 }

(The geometry is the UK as taken from https://github.com/johan/world.geo.json/blob/master/countries.geo.json and would be replaced with the Leaflet bounding box when we run the query dynamically.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MongoDB We have 2 mongodb collections: one for current products, and one for obsolete products 📍🏭 Packager codes https://blog.openfoodfacts.org/en/news/discover-what-food-products-are-made-near-you-with-made-near-
Projects
None yet
Development

No branches or pull requests

1 participant