Skip to content

Commit

Permalink
handle wkb
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterprovoost committed Jan 6, 2025
1 parent 0d63938 commit 5fb4e04
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ function App() {

<footer className="footer mt-auto pt-5 pb-4 bg-light">
<Container>
<p className="text-muted">This page parses, visualizes, and shares <a href="https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry" rel="noreferrer" className="text-muted" target="_blank">WKT</a> (ISO 13249) as well as <a href="https://opengeospatial.github.io/ogc-geosparql/geosparql11/spec.html#_rdfs_datatype_geowktliteral" target="blank" rel="noreferrer" className="text-muted">geo:wktLiteral</a> strings in a variety of coordinate reference systems. Built with <a href="https://openlayers.org/" target="blank" rel="noreferrer" className="text-muted">OpenLayers</a>, <a href="https://leafletjs.com/" target="blank" rel="noreferrer" className="text-muted">Leaflet</a>, <a href="https://trac.osgeo.org/proj4js" target="blank" rel="noreferrer" className="text-muted">Proj4js</a>, <a href="https://github.com/terraformer-js/terraformer" target="blank" rel="noreferrer" className="text-muted">terraformer</a>, and <a href="https://epsg.io/" target="blank" rel="noreferrer" className="text-muted">epsg.io</a>. Use the drawing tools to create your own geometries. Copy as Well-known Binary (WKB) or Extended Well-known Binary (EWKB). Also supports <a href="https://h3geo.org/" rel="noreferrer" className="text-muted" target="_blank">Uber H3</a>, <a href="https://en.wikipedia.org/wiki/Geohash" rel="noreferrer" className="text-muted" target="_blank">Geohash</a>, <a href="https://learn.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system" rel="noreferrer" className="text-muted" target="_blank">Quadkey</a>, and WFS BBOX conversion to WKT.</p>
<p className="text-muted">This page parses, visualizes, and shares <a href="https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry" rel="noreferrer" className="text-muted" target="_blank">WKT</a> (ISO 13249) as well as <a href="https://opengeospatial.github.io/ogc-geosparql/geosparql11/spec.html#_rdfs_datatype_geowktliteral" target="blank" rel="noreferrer" className="text-muted">geo:wktLiteral</a> strings in a variety of coordinate reference systems. Built with <a href="https://openlayers.org/" target="blank" rel="noreferrer" className="text-muted">OpenLayers</a>, <a href="https://leafletjs.com/" target="blank" rel="noreferrer" className="text-muted">Leaflet</a>, <a href="https://trac.osgeo.org/proj4js" target="blank" rel="noreferrer" className="text-muted">Proj4js</a>, <a href="https://github.com/terraformer-js/terraformer" target="blank" rel="noreferrer" className="text-muted">terraformer</a>, and <a href="https://epsg.io/" target="blank" rel="noreferrer" className="text-muted">epsg.io</a>. Use the drawing tools to create your own geometries. Copy as Well-known Binary (WKB) or Extended Well-known Binary (EWKB). Also supports <a href="https://h3geo.org/" rel="noreferrer" className="text-muted" target="_blank">Uber H3</a>, <a href="https://en.wikipedia.org/wiki/Geohash" rel="noreferrer" className="text-muted" target="_blank">Geohash</a>, <a href="https://learn.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system" rel="noreferrer" className="text-muted" target="_blank">Quadkey</a>, WKB, and WFS BBOX conversion to WKT.</p>
<p className="text-muted">Created by <Twitter className="mb-1"/> <a rel="noreferrer" className="text-muted" href="https://twitter.com/PieterPrvst" target="_blank">PieterPrvst</a></p>
</Container>
</footer>
Expand Down
17 changes: 15 additions & 2 deletions src/wkt.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ function isBbox(input) {
}

function isGeohash(input) {
// TODO: handle WKB as well
return input.wkt && input.wkt.match(/^[0-9a-z]+$/);
return input.wkt && input.wkt.length <= 10 && input.wkt.match(/^[0-9a-z]+$/);
}

function isWkb(input) {
return input.wkt && input.wkt.match(/^[0-9a-fA-F]+$/);
}

function h3ToWkt(h3) {
Expand Down Expand Up @@ -164,6 +167,12 @@ function bboxToWkt(bbox) {
"))";
}

function wkbToWkt(wkb) {
const buffer = Buffer.from(wkb, "hex");
const geometry = Geometry.parse(buffer);
return geometry.toWkt();
}

function handleOtherFormats(input) {
if (isH3(input)) {
input.wkt = h3ToWkt(input.wkt);
Expand All @@ -181,6 +190,10 @@ function handleOtherFormats(input) {
input.wkt = geohashToWkt(input.wkt);
input.epsg = 4326;
return("Converted Geohash to WKT");
} else if (isWkb(input)) {
input.wkt = wkbToWkt(input.wkt);
input.epsg = 4326;
return("Converted WKB to WKT");
} else {
return null;
}
Expand Down
7 changes: 7 additions & 0 deletions src/wkt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ describe("handleOtherFormats", () => {
const message = handleOtherFormats(input);
expect(message).toEqual("Converted Geohash to WKT");
});
it("handles WKB", async () => {
const input = {
wkt: "01010000001343723271CB094047E4BB94BA9A4940"
};
const message = handleOtherFormats(input);
expect(message).toEqual("Converted WKB to WKT");
});
});

0 comments on commit 5fb4e04

Please sign in to comment.