npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

geo-polygon-utils

v1.0.0

Published

Efficient polygon data handling utilities for React and Next.js applications

Downloads

4

Readme

Polygon Utils

A high-performance polygon data handling library for React and Next.js applications.

Features

  • 🚀 Performance Optimized: Simplify and flatten complex polygon data for better rendering performance
  • 🧠 Smart Caching: Includes built-in memoization to prevent redundant processing
  • 📦 Small Footprint: Minimal bundle size with tree-shakable exports
  • 🔄 Real-time Ready: Designed for applications that handle frequent polygon data updates
  • 🗺️ Map Integration: Works with any map library (Leaflet, Mapbox, Google Maps, etc.)
  • ⚛️ React Hooks: Purpose-built hooks for React applications

Installation

npm install polygon-utils
# or
yarn add polygon-utils
# or
pnpm add polygon-utils

Quick Start

import { usePolygonData, PolygonMap } from "polygon-utils";
import { MapContainer, TileLayer } from "react-leaflet";

function MyMap() {
  // Fetch and automatically optimize polygon data
  const { data, loading, error } = usePolygonData("/api/polygons", {
    simplify: true,
    flatten: true,
    pollingInterval: 30000, // Poll every 30 seconds
  });

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;
  if (!data) return <div>No polygon data available</div>;

  return (
    <MapContainer center={[51.505, -0.09]} zoom={13}>
      <TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />

      {/* Render optimized polygons */}
      <PolygonMap
        polygons={data}
        renderPolygon={(polygon, index) => (
          <Polygon positions={polygon.coordinates} key={index} color="blue" />
        )}
      />
    </MapContainer>
  );
}

Core Utilities

Polygon Processing

import {
  flattenMultiPolygon,
  simplifyPolygon,
  mergePolygons,
  calculateArea,
  isPointInPolygon,
  optimizeForRendering,
} from "polygon-utils";

// Simplify a complex polygon
const simplifiedPolygon = simplifyPolygon(polygon, 0.001);

// Convert a MultiPolygon to a simple Polygon
const flattenedPolygon = flattenMultiPolygon(multiPolygon);

// Merge multiple polygons into one
const merged = mergePolygons([polygon1, polygon2, polygon3]);

// Check if a point is inside a polygon
const isInside = isPointInPolygon([longitude, latitude], polygon);

// Get polygon area in square meters
const area = calculateArea(polygon);

// All-in-one optimization
const optimized = optimizeForRendering(polygon, {
  simplify: true,
  simplifyTolerance: 0.001,
  flatten: true,
});

React Hooks

usePolygonData

Fetches and optimizes polygon data from an API endpoint.

const { data, loading, error, refresh } = usePolygonData(url, {
  flatten: true, // Flatten MultiPolygons
  simplify: true, // Simplify polygons
  simplifyTolerance: 0.001, // Tolerance level (0-1)
  pollingInterval: 0, // Auto-refresh interval (ms, 0 to disable)
  autoOptimize: true, // Apply optimizations automatically
});

usePolygonOperations

Provides optimized, memoized polygon operation functions.

const { simplify, flatten, merge, area, contains } = usePolygonOperations({
  defaultSimplifyTolerance: 0.001, // Default simplification level
  useMemoization: true, // Use caching for better performance
  cacheSize: 50, // Size of the operation cache
});

// These operations are now optimized with memoization
const simplified = simplify(polygon);
const isInside = contains([longitude, latitude], polygon);

React Components

OptimizedPolygon

Component that automatically optimizes a polygon for rendering.

<OptimizedPolygon
  data={polygon}
  autoOptimize={true}
  simplify={true}
  simplifyTolerance={0.001}
  flatten={true}
>
  {(optimizedPolygon) => <YourMapPolygonComponent data={optimizedPolygon} />}
</OptimizedPolygon>

PolygonMap

Efficiently renders multiple polygons with optimizations.

<PolygonMap
  polygons={polygonArray}
  renderPolygon={(polygon, index) => (
    <YourMapPolygonComponent data={polygon} key={index} />
  )}
  autoOptimize={true}
  simplify={true}
  simplifyTolerance={0.001}
  flatten={true}
/>

Performance Tips

  1. Use Memoization: The built-in caching system prevents redundant calculations
  2. Adjust Tolerance: Higher simplifyTolerance values (e.g., 0.01) create simpler polygons but with less precision
  3. Flatten MultiPolygons: Converting complex nested MultiPolygons to simpler Polygons improves rendering speed
  4. Batch Processing: Use mergePolygons to combine many small polygons before rendering
  5. ID-based Caching: Provide stable IDs to improve cache hit rates

License

MIT

polygon-utils