Code Monkey home page Code Monkey logo

olrandompointsfill's Introduction

OLRandomPointsFill

Fill polygons in OpenLayers using random point features

A utility class for OpenLayers to generate and display semi-random points within polygonal features. This class is designed to work with OpenLayers maps and provides functionality to fill polygon features with points (can be a symbol) which are arranged on a grid and with a random shift. Here I'm using it to display forest cover, consisting of multiple polygons.

image

Method:

  • fillWithRandomPoints: Main method to generate and render random points within polygons.

Usage: 0. Create a PMTiles file from your polygons with Tippecanoe using low detail for low zoom levels and -ac to coalesce tiny features. Example:

tippecanoe output.pmtiles input.geojson -Z9 -z14 -D7 -d10 -M50000 -ac

PMTiles coalesced polygons at low zoom (grey) vs original forest cover polygons (green)

coalesced_zoom_1

  1. Import the class:

    import { RandomPointsFill } from "./RandomPointsFill.js";

  2. Create styles for your fill and put them in an array, i.e.


const iconoBosquePrimario = new Style({
  image: new Icon({
    src: "./data/iconos/arbol.svg",
    scale: 0.018,
  }),
});

const iconoBosque = new Style({
  image: new Icon({
    src: "./data/iconos/arbol.svg",
    scale: 0.012,
  }),
});

const iconoManglar = new Style({
  image: new Icon({
    src: "./data/iconos/manglar.svg",
    scale: 0.05,
  }),
});

const estiloBosques = [iconoBosquePrimario, iconoBosque, iconoManglar];

  1. Add your polygon (PMTiles) and point fill layers. You may want to make your polygons transparent.
const fuenteCobertura = new PMTilesVectorSource({
  url: "./data/cobertura.pmtiles",
});

const capaCobertura = new VectorTile({

  source: fuenteCobertura,
  visible: true,
  style: new Style({
    stroke: new Stroke({
      color: "#00000000",
      width: 0,
    }),
    fill: new Fill({
      color: "#00000000",
    }),
  }),

});

const fuenteArbolitos = new VectorSource();

const capaArbolitos = new VectorLayer({
  source: fuenteArbolitos,
});

  1. Initialize the RandomPointsFill class with the OpenLayers map instance, feature source (thhe polygons you want to fill), the fill source (a vector source), and point style.
const arbolitos = new RandomPointsFill(
  mapa,
  fuenteCobertura,
  fuenteArbolitos,
  estiloBosques
);
  1. Call the fillWithRandomPoints method, typically as a response to polygon vector layer events such as 'postrender'.
 const randomPointsFill = new RandomPointsFill(mapa, fuenteCobertura, fuenteArbolitos, estiloBosques);
 fuenteCobertura.on('postrender', (event) => randomPointsFill.fillWithRandomPoints(event));

pointStyle is an array with various possible point styles.

TODO: currently filtering and classification is hardcoded, add filtering and density, randomness as a parameter when calling the class

olrandompointsfill's People

Contributors

mir123 avatar

Watchers

 avatar

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.