Skip to content

Commit

Permalink
review how we save json + gpx and rainbow
Browse files Browse the repository at this point in the history
  • Loading branch information
aloxe committed Jun 20, 2024
1 parent 1857f50 commit 171ca8e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/components/FileUpload.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";
import Dropzone from "react-dropzone";
import {getDistance, loadGeoJsonFromGpx, renameGpx, uploadFile, uploadJson } from "../helpers/gpxUtil";
import { calcColor, toSlug } from "../helpers/strings";
import { toSlug } from "../helpers/strings";
import Login from "./Login";
import './FileUpload.css'

Expand All @@ -16,13 +16,18 @@ const FileUpload = ({ isLogged, setIsLogged, setGeojsonList }) => {
title: '',
countries: [],
});


const calcColor = (length, val) => {
var minHue=320, maxHue=0;
var curPercent = (val-1)/(length-1);
return `hsl(${Math.round((curPercent*(maxHue-minHue))+minHue)},100%,50%)`;
}

useEffect(() => {
const getGeoJsonAwaited = async (name) => {
const geojson = await loadGeoJsonFromGpx(name);
const l = geojson.features;
const l = geojson.features.length;
geojson.features.map((feature, i) => feature.properties.color = calcColor(l,i));
geojson.color = "rainbow";
setCurrentGeoJson(geojson);
setGeojsonList([geojson]);
}
Expand Down Expand Up @@ -71,8 +76,9 @@ const FileUpload = ({ isLogged, setIsLogged, setGeojsonList }) => {
currentGeoJson.title = form.title;
currentGeoJson.countries = form.countries;
currentGeoJson.distance = getDistance(currentGeoJson)
currentGeoJson.slug = `${currentGeoJson.date}-${toSlug(currentGeoJson.title)}.${currentGeoJson.countries.toString().replace(',','.')}`
await renameGpx(currentName, currentGeoJson.slug)
currentGeoJson.slug = `${currentGeoJson.date}-${toSlug(currentGeoJson.title)}`
const geoslug = `${currentGeoJson.slug}.${currentGeoJson.countries.toString().replace(',','.')}`
await renameGpx(currentName, geoslug)
const saved = await uploadJson(currentGeoJson)
if (saved.status === 201) {
setMessage(`💾 ${saved.filename}`)
Expand Down
2 changes: 1 addition & 1 deletion src/components/MainMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const MainMap = ({geojsonList, tileName, handleClickPopup, currentGeoJson}) => {
// use patch-package to update pigeon-maps
return { strokeWidth: "0", stroke: "black", r: '0', };
}
if (geojson.color === "rainbow") {
if (geojson.features.length > 1) { // rainbow colours for multi paths geojson
return {
strokeWidth: "2",
stroke: feature.properties.color,
Expand Down
1 change: 1 addition & 0 deletions src/helpers/gpxUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const loadGeoJsonFromGpx = async (url) => {
}

export const uploadJson = async (geodata) => {
console.log("upload", geodata);
const body = JSON.stringify(geodata)
const response = await axios.post('https://alix.guillard.fr/data/velo/api/uploadjson.php', body, {
headers: { 'Content-Type': 'application/json' }
Expand Down
5 changes: 0 additions & 5 deletions src/helpers/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,4 @@ export const toSlug = (str) => {
.replace(/-[-]+/g, "-");
}

export const calcColor = (length, val) => {
var minHue=240, maxHue=0;
var curPercent = (val)/(length);
return `hsl(${((curPercent*(maxHue-minHue))+minHue).parseInt()},100%,50%)`;
}

0 comments on commit 171ca8e

Please sign in to comment.