From dc14e652f730f1b05b0e4801542a53a8a99baa51 Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Mon, 3 Feb 2020 17:06:55 +0100 Subject: [PATCH] Work on polygon benchmark --- README.md | 1 + package.json | 1 + public/runtime-config.js | 20 ++++++++++---------- src/database.js | 41 ++++++++++++++++++++++++++++++++++++++++ yarn.lock | 36 +++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 57791e0..14ba42e 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ - which-polygon - supercluster +- @spatial/points-within-polygon # Create React App parts diff --git a/package.json b/package.json index 10ae7a6..1cc4cbb 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", + "@turf/points-within-polygon": "^5.1.5", "alasql": "^0.5.4", "console": "^0.7.2", "date-fns": "^2.0.0", diff --git a/public/runtime-config.js b/public/runtime-config.js index c36dd2b..c0a42ff 100644 --- a/public/runtime-config.js +++ b/public/runtime-config.js @@ -1,18 +1,18 @@ // Away config -// window['runtimeConfig'] = { -// digikamDbUrl: "https://sandervocke-nas.duckdns.org/digikam-fatclient-data/digikam4.db", -// photosUrl: "https://sandervocke-nas.duckdns.org/digikam-fatclient-data/photos", -// thumbsUrl: "https://sandervocke-nas.duckdns.org/digikam-fatclient-data/thumbs", -// } - -// Home config window['runtimeConfig'] = { - digikamDbUrl: "https://192.168.1.101/digikam-fatclient-data/digikam4.db", - photosUrl: "https://192.168.1.101/digikam-fatclient-data/photos", - thumbsUrl: "https://192.168.1.101/digikam-fatclient-data/thumbs", + digikamDbUrl: "https://sandervocke-nas.duckdns.org/digikam-fatclient-data/digikam4.db", + photosUrl: "https://sandervocke-nas.duckdns.org/digikam-fatclient-data/photos", + thumbsUrl: "https://sandervocke-nas.duckdns.org/digikam-fatclient-data/thumbs", } +// Home config +// window['runtimeConfig'] = { +// digikamDbUrl: "https://192.168.1.101/digikam-fatclient-data/digikam4.db", +// photosUrl: "https://192.168.1.101/digikam-fatclient-data/photos", +// thumbsUrl: "https://192.168.1.101/digikam-fatclient-data/thumbs", +// } + // Production config // window['runtimeConfig'] = { // digikamDbUrl: "/digikam-fatclient-data/digikam4.db", diff --git a/src/database.js b/src/database.js index 62585f7..aeacb5b 100644 --- a/src/database.js +++ b/src/database.js @@ -3,6 +3,8 @@ import NodeEnvironment from 'jest-environment-node'; import { add_geo_area_to_store, get_geo_area_from_store } from './geo_store.js'; +import pointsWithinPolygon from '@turf/points-within-polygon'; + export async function sqljs_async_queries(sqljs_object, queries) { //var t0 = performance.now(); for (let i = 0; i < (queries.length - 1); i++) { @@ -105,6 +107,44 @@ export async function add_full_tag_info(db) { return db; } +function polygons_benchmark(database) { + var img_query = "SELECT Images.id, ImagePositions.latitudeNumber, ImagePositions.longitudeNumber FROM Images " + + "LEFT JOIN ImagePositions ON ImagePositions.imageid=Images.id GROUP BY Images.id;"; + + sqljs_async_queries(database, [img_query]).then(res => { + fetch("https://nominatim.openstreetmap.org/search?polygon_geojson=1&polygon_threshold=0.001&format=json&limit=5&q=Australia") + .then(res => res.json()) + .then(jsonres => { + var geojson; + var points = []; + + console.log("Nominatim geo answer:", jsonres); + + if (Array.isArray(jsonres) && jsonres.length > 0) { + geojson = jsonres[0].geojson; + } + + if (res && Array.isArray(res) && res.length > 0) { + var cols = res[0].columns; + var data = res[0].values; + data.forEach(row => { + points.push([row[cols.indexOf("longitudeNumber")], row[cols.indexOf("latitudeNumber")]]); + }); + } + + console.log("Points: ", points); + console.log("GEOJSON: ", geojson); + + console.time("points within polygon"); + var found = pointsWithinPolygon(points, geojson); + console.timeEnd("points within polygon"); + + console.log("PointsWithinPolygon: ", found); + }); + }) + .catch(err => { throw err; }); +} + export function ProvideDB(props) { const { children, db_url } = props; const [db, setDb] = useState(null); @@ -116,6 +156,7 @@ export function ProvideDB(props) { add_full_tag_info(db).then((newdb) => { db.create_function("REGEXP", regexp_match); db.create_function("IS_IN_GEO", is_in_geo_polygon_from_store); + polygons_benchmark(db); setError(false); setDb(newdb); }) diff --git a/yarn.lock b/yarn.lock index 36058f7..7fe7f21 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1508,6 +1508,42 @@ resolved "https://registry.yarnpkg.com/@testing-library/user-event/-/user-event-7.2.1.tgz#2ad4e844175a3738cb9e7064be5ea070b8863a1c" integrity sha512-oZ0Ib5I4Z2pUEcoo95cT1cr6slco9WY7yiPpG+RGNkj8YcYgJnM7pXmYmorNOReh8MIGcKSqXyeGjxnr8YiZbA== +"@turf/boolean-point-in-polygon@^5.1.5": + version "5.1.5" + resolved "https://registry.yarnpkg.com/@turf/boolean-point-in-polygon/-/boolean-point-in-polygon-5.1.5.tgz#f01cc194d1e030a548bfda981cba43cfd62941b7" + integrity sha1-8BzBlNHgMKVIv9qYHLpDz9YpQbc= + dependencies: + "@turf/helpers" "^5.1.5" + "@turf/invariant" "^5.1.5" + +"@turf/helpers@^5.1.5": + version "5.1.5" + resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-5.1.5.tgz#153405227ab933d004a5bb9641a9ed999fcbe0cf" + integrity sha1-FTQFInq5M9AEpbuWQantmZ/L4M8= + +"@turf/invariant@^5.1.5": + version "5.2.0" + resolved "https://registry.yarnpkg.com/@turf/invariant/-/invariant-5.2.0.tgz#f0150ff7290b38577b73d088b7932c1ee0aa90a7" + integrity sha1-8BUP9ykLOFd7c9CIt5MsHuCqkKc= + dependencies: + "@turf/helpers" "^5.1.5" + +"@turf/meta@^5.1.5": + version "5.2.0" + resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-5.2.0.tgz#3b1ad485ee0c3b0b1775132a32c384d53e4ba53d" + integrity sha1-OxrUhe4MOwsXdRMqMsOE1T5LpT0= + dependencies: + "@turf/helpers" "^5.1.5" + +"@turf/points-within-polygon@^5.1.5": + version "5.1.5" + resolved "https://registry.yarnpkg.com/@turf/points-within-polygon/-/points-within-polygon-5.1.5.tgz#2b855a5df3aada57c2ee820a0754ab94928a2337" + integrity sha1-K4VaXfOq2lfC7oIKB1SrlJKKIzc= + dependencies: + "@turf/boolean-point-in-polygon" "^5.1.5" + "@turf/helpers" "^5.1.5" + "@turf/meta" "^5.1.5" + "@types/babel__core@^7.1.0": version "7.1.3" resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.3.tgz#e441ea7df63cd080dfcd02ab199e6d16a735fc30"