Skip to content

Commit

Permalink
copy as geojson
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterprovoost committed Jun 22, 2024
1 parent 59f89c1 commit d7442d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
11 changes: 2 additions & 9 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "leaflet/dist/leaflet.css";
import "leaflet-draw/dist/leaflet.draw.css";
import { React, useState, useMemo, useEffect, useRef } from "react";
import examples from "./examples";
import proj4 from "proj4";
import { Twitter } from "react-bootstrap-icons";
import FullscreenControl from "./FullscreenControl";
import CRC32 from "crc-32";
Expand All @@ -21,8 +20,8 @@ const formats = {
"wkt": "WKT",
"wkb": "WKB",
"ewkb": "EWKB",
// "geojson": "GeoJSON",
"bbox": "BBOX"
"bbox": "BBOX",
"geojson": "GeoJSON"
};

function createCircleMarker(feature, latlng) {
Expand Down Expand Up @@ -286,12 +285,6 @@ function App() {
const conf = {
pointToLayer: createCircleMarker,
};
if (spatial.proj) {
conf.coordsToLatLng = function(coords) {
const newCoords = proj4(spatial.proj, "EPSG:" + DEFAULT_EPSG, [coords[0], coords[1]]);
return new L.LatLng(newCoords[1], newCoords[0]);
}
}
let newLayer = L.geoJSON(spatial.json, conf).addTo(groupRef.current);
if (map) map.flyToBounds(newLayer.getBounds(), { duration: 0.5, maxZoom: 14 });
}
Expand Down
28 changes: 21 additions & 7 deletions src/wkt.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { cellToBoundary } from "h3-js";
import geohash from "ngeohash";
import quadkeytools from "quadkeytools";
import { geojsonToWKT } from "@terraformer/wkt";
import proj4 from "proj4";
import {register} from "ol/proj/proj4";

const USE_WKT = false;

Expand All @@ -18,11 +20,15 @@ class ValueError extends Error {
}
}

function parseWkt(wkt) {
function parseWkt(wkt, input) {
proj4.defs("EPSG:" + input.epsg, input.proj);
register(proj4);
const options = {"dataProjection": "EPSG:" + input.epsg, "featureProjection": "EPSG:" + input.epsg};
const wktFormat = new WKT();
const feature = wktFormat.readFeature(wkt);
const geojsonFormat = new GeoJSON({});
const json = geojsonFormat.writeFeatureObject(feature);
const feature = wktFormat.readFeature(wkt, options);
feature.getGeometry().transform("EPSG:" + input.epsg, "EPSG:4326");
const geojsonFormat = new GeoJSON();
const json = geojsonFormat.writeFeatureObject(feature, options);
return json;
}

Expand Down Expand Up @@ -155,8 +161,12 @@ async function transformInput(input) {

// split input, parse EPSG if in WKT

console.log(input)

const { wktPart, parsedEpsg } = extractAndParseCrs(input);


console.log(input)

if (parsedEpsg) {
input = {
...input,
Expand All @@ -166,16 +176,20 @@ async function transformInput(input) {

// get proj

let epsgInt = parseInt(input.epsg);
if (!epsgInt || epsgInt < 1024 || epsgInt > 32767) {
throw new ValueError("Invalid EPSG");
}
input.proj = await fetchProj(input.epsg);
if (!input.proj) {
throw ValueError("EPSG not found");
throw new ValueError("EPSG not found");
}

// parse WKT

if (input.proj && wktPart !== "") {
try {
input.json = parseWkt(wktPart);
input.json = parseWkt(wktPart, input);

// TODO: move
const uint8 = Geometry.parse(wktPart).toWkb();
Expand Down

0 comments on commit d7442d4

Please sign in to comment.