Code Monkey home page Code Monkey logo

turf-node-example's People

Contributors

deniscarriere avatar lyzidiamond avatar tcql avatar tmcw avatar tsamaya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

turf-node-example's Issues

screencast?

How is this better explainable than a cloned project? Unclear

Please add a license file

I can't use a library without knowing its license.

Will you be licensing under MIT license? Or Apache License?

How can I use Douglas Peucker's algorithm using the turf library in Google Maps for Polygons?

I have kml files with more than 10000 polygons drawn, loading takes a long time, I'm trying to reduce loading with this algorithm, but I don't know how to start.

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,
  };
};

Update to turf 2.0.0?

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.