turfjs / turf-node-example Goto Github PK
View Code? Open in Web Editor NEWan example of how to use Turf for a scripting task with node.
an example of how to use Turf for a scripting task with node.
How is this better explainable than a cloned project? Unclear
I can't use a library without knowing its license.
Will you be licensing under MIT license? Or Apache License?
import turfSimplify from '@turf/simplify';
import turf from '@turf/turf';
import { useEffect, useState } from 'react';
import { computeChanges } from '../../../helpers/google-maps';
import {
ColorPaletteStateNumber,
DataPolygon,
PALETTE,
Polygon,
polygonToJstsPolygon,
} from './commons';
export type UseGoogleMapsPolygonsType = {
readonly dataPolygons: DataPolygon[];
};
export type UseGoogleMapsPolygonsCreateType = {
mapInstance: google.maps.Map<Element> | null;
onPolygonClicked?(polygon: Polygon): void;
polygons?: Polygon[];
};
const getPolygonColor = (polygon: Polygon): string => {
const palette = polygon.colorPalette
? PALETTE[polygon.colorPalette]
: PALETTE.default;
if (polygon.highlight) return palette.highlight;
if (polygon.disabled) return palette.disabled;
return palette.normal;
};
const getPolygonNumber = (
polygon: Polygon,
field: ColorPaletteStateNumber,
): number => {
const palette = polygon.colorPalette
? PALETTE[polygon.colorPalette]
: PALETTE.default;
return palette[field];
};
export const useGoogleMapsPolygons = ({
mapInstance,
onPolygonClicked,
polygons,
}: UseGoogleMapsPolygonsCreateType): UseGoogleMapsPolygonsType => {
const [dataPolygons, setDataPolygons] = useState<DataPolygon[]>([]);
useEffect(() => {
if (polygons) {
const result = computeChanges<Polygon, DataPolygon>(
polygons,
dataPolygons,
);
if (result.hasChanges()) {
const newDataPolygons: DataPolygon[] = [];
dataPolygons.forEach(dataPolygon => {
if (result.idsToRemove.includes(dataPolygon.targetId)) {
dataPolygon.mapPolygon.setMap(null);
} else {
window.google.maps.event.clearListeners(
dataPolygon.mapPolygon,
'click',
);
if (onPolygonClicked) {
dataPolygon.mapPolygon.addListener('click', () =>
onPolygonClicked(dataPolygon.data),
);
}
newDataPolygons.push(dataPolygon);
}
});
result.itemsToUpdate.forEach(item => {
const dataPolygon = dataPolygons.find(
searchItem => searchItem.targetId === item.id,
);
if (dataPolygon) {
dataPolygon.data = item;
dataPolygon.lastModified = item.lastModified;
dataPolygon.mapPolygon.setOptions({
paths: item.coordinates,
strokeColor: getPolygonColor(item),
strokeOpacity: getPolygonNumber(item, 'strokeOpacity'),
strokeWeight: 3,
fillColor: getPolygonColor(item),
fillOpacity: getPolygonNumber(item, 'fillOpacity'),
zIndex: 1000,
});
dataPolygon.jstsPolygon = polygonToJstsPolygon(
dataPolygon.mapPolygon,
);
dataPolygon.mapPolygon.setMap(mapInstance);
}
});
result.itemsToAdd.forEach(item => {
const mapPolygon = new window.google.maps.Polygon({
paths: turf.simplify(item.coordinates),
strokeColor: getPolygonColor(item),
strokeOpacity: getPolygonNumber(item, 'strokeOpacity'),
strokeWeight: 3,
fillColor: getPolygonColor(item),
fillOpacity: getPolygonNumber(item, 'fillOpacity'),
});
mapPolygon.setMap(mapInstance);
if (onPolygonClicked) {
mapPolygon.addListener('click', () => onPolygonClicked(item));
}
newDataPolygons.push({
targetId: item.id,
lastModified: item.lastModified,
data: item,
mapPolygon,
jstsPolygon: polygonToJstsPolygon(mapPolygon),
});
});
setDataPolygons(newDataPolygons);
} else {
dataPolygons.forEach(dataPolygon => {
window.google.maps.event.clearListeners(
dataPolygon.mapPolygon,
'click',
);
if (onPolygonClicked) {
dataPolygon.mapPolygon.addListener('click', () =>
onPolygonClicked(dataPolygon.data),
);
}
dataPolygon.mapPolygon.setMap(mapInstance);
});
}
} else {
dataPolygons.forEach(dataPolygon => dataPolygon.mapPolygon.setMap(null));
}
// atribuir o mapa para nulo no unmount estava causando "piscadas" do mapa
// nós não precisamos atribuir para nulo de fato, uma vez que criamos
// uma instância de mapa em cada um dos componentes
// também não parece que haverá vazamento de memória não fazendo a atribuição
// o mesmo acontece para os marcadores
/* return () =>
dataPolygons.forEach(dataPolygon => dataPolygon.mapPolygon.setMap(null)); */
}, [polygons, dataPolygons, mapInstance, onPolygonClicked]);
return {
dataPolygons,
};
};
I clone the repo and ran npm install the first time and received the following error message:
npm ERR! EEXIST, open '/home/skors/.npm/5551a734-e-skors-npm-rx-2-4-9-package-tgz.lock' File exists: /home/skors/.npm/5551a734-e-skors-npm-rx-2-4-9-package-tgz.lock Move it away, and try again. npm ERR! System Linux 3.13.0-49-generic npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" npm ERR! cwd /home/skors/gis/turf-node-example npm ERR! node -v v0.10.38 npm ERR! npm -v 1.4.28 npm ERR! path /home/skors/.npm/5551a734-e-skors-npm-rx-2-4-9-package-tgz.lock npm ERR! code EEXIST npm ERR! errno 47 npm ERR! not ok code 0
I went into the path specified in the error message and there was no file with that name.
I had no idea what to do next. Since turf was updated to 2.0.0 since the last release of this, I thought the new turf could maybe fix it. So I changed turf's requirement in package.json to 2.0.0
rm -rf'ed node_module folder and ran npm install again, I received no errors, and was able to run this module with no problems.
Since the error message referred to a locked npm file and nothing to do with turf, I don't know whether the problem was caused by an outdated turf module.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.