Skip to content

Commit

Permalink
Use wellknown convert geojson wkt
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Nov 7, 2024
1 parent c7783b3 commit 5b4650a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ <h1 id="taxonName"></h1>
integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4"
crossorigin="anonymous"
></script>
<script src=" https://cdn.jsdelivr.net/npm/wellknown@0.5.0/wellknown.min.js "></script>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js"></script>
<script src="js/status.js"></script>
<script src="js/utils.js"></script>
Expand Down
43 changes: 15 additions & 28 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,6 @@ function getQueryParams() {
};
}

/**
* Converts a GeoJSON feature to a Well-Known Text (WKT) string.
* @param {Object} feature - A GeoJSON feature object with geometry containing coordinates.
* @returns {string} - A WKT string representing the polygon.
*/
function convertGeoJsonToWkt(feature) {
const coo = feature.geometry.coordinates
.map(function (ring) {
return (
"(" +
ring
.map(function (p) {
return p[0] + " " + p[1];
})
.join(", ") +
")"
);
})
.join(", ");
const wkt_str = "POLYGON(" + coo + ")";
return wkt_str;
}

/**
* Given a params object, returns a WKT string representing a localisation.
Expand All @@ -109,20 +87,29 @@ function convertGeoJsonToWkt(feature) {
* @param {Object} params - An object containing localisation parameters.
* @returns {string|undefined} - A WKT string or undefined.
*/


function processLocalisation(params) {
if (params.wkt) {
return params.wkt;
const geojson = wellknown.parse(params.wkt)
//Test geom type :
if (geojson.type.endsWith('Polygon')) {
return params.wkt;
}
else {
// Buffer
const buffered = turf.buffer(geojson, params.radius | 100, { units: "meters" });
return wellknown.stringify(buffered)
}
}
if (params.x && params.x) {
const point = turf.point([params.x, params.x]);
const buffered = turf.buffer(point, params.radius | 100, {
units: "meters",
});
//convert the json-input to WKT
return convertGeoJsonToWkt(buffered);
const buffered = turf.buffer(point, params.radius | 100, { units: "meters" });
return wellknown.stringify(buffered)
}
}


/**
* Completes the data with the status and the picture of each taxon in the provided list.
* @param {Array<Object>} taxonsData - An array of taxon data objects.
Expand Down

0 comments on commit 5b4650a

Please sign in to comment.