Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
viewport-mercator-project
Advanced tools
Convert to and from lat/lng and pixels in web mercator at arbitrary floating point zoom levels.
The viewport-mercator-project npm package is a utility library for performing viewport-related calculations in web mapping applications. It is particularly useful for converting between geographic coordinates and screen coordinates, handling zoom levels, and managing map projections.
Project geographic coordinates to screen coordinates
This feature allows you to project geographic coordinates (longitude, latitude) to screen coordinates (x, y) based on the current viewport settings.
const { WebMercatorViewport } = require('viewport-mercator-project');
const viewport = new WebMercatorViewport({
width: 800,
height: 600,
longitude: -122.45,
latitude: 37.78,
zoom: 12
});
const screenCoords = viewport.project([-122.45, 37.78]);
console.log(screenCoords); // [400, 300]
Unproject screen coordinates to geographic coordinates
This feature allows you to convert screen coordinates (x, y) back to geographic coordinates (longitude, latitude) based on the current viewport settings.
const { WebMercatorViewport } = require('viewport-mercator-project');
const viewport = new WebMercatorViewport({
width: 800,
height: 600,
longitude: -122.45,
latitude: 37.78,
zoom: 12
});
const geoCoords = viewport.unproject([400, 300]);
console.log(geoCoords); // [-122.45, 37.78]
Get bounding box of the viewport
This feature allows you to get the geographic bounding box of the current viewport, which can be useful for determining the area currently visible on the map.
const { WebMercatorViewport } = require('viewport-mercator-project');
const viewport = new WebMercatorViewport({
width: 800,
height: 600,
longitude: -122.45,
latitude: 37.78,
zoom: 12
});
const bounds = viewport.getBounds();
console.log(bounds); // [[-122.519, 37.704], [-122.381, 37.856]]
Mapbox GL JS is a powerful library for interactive, customizable vector maps on the web. It provides similar functionalities for projecting and unprojecting coordinates, but also includes a full-featured map rendering engine and extensive styling options.
Leaflet is a popular open-source JavaScript library for mobile-friendly interactive maps. It offers similar coordinate transformation functionalities, but is more focused on ease of use and simplicity, making it a good choice for basic mapping applications.
Proj4 is a library for performing cartographic transformations, including coordinate projections. It is more specialized in handling various map projections and transformations, making it a good complement to viewport-mercator-project for advanced geospatial applications.
Utility for converting to and from latitude and longitude coordinates, given a map viewport projection. Initially built for use with react-map-gl but useful for most web mapping libraries that support floating point zoom levels.
// Create a new viewport.
var ViewportMercator = require('viewport-mercator-project');
// NOTE: The `viewport` object returned from `ViewportMercator` are immutable by design.
var viewport = ViewportMercator({
longitude: 0,
latitude: 0,
zoom: 0,
width: 600,
height: 500
});
// A longitude, latitude pair as an array.
var lnglat = [0, 0];
var pixels = viewport.project(lnglat); // returns [300, 400]
// A width, height pair as an array.
viewport.unproject(pixels); // returns [0, 0]
// Test if a lnglat is within the viewport
viewport.contains(lnglat); // true
npm install viewport-mercator-project --save
npm run test
The coordinate system of the viewport is defined as a cartesian plane with the origin in the top left, where the positive x-axis goes right, and the positive y-axis goes down.
That is, the top left corner is [0, 0]
and the bottom right corner is [width, height]
.
center
property was broken up into longitude
, latitude
properties.
dimensions
property was broken up into width
, and height
properties.
Here's what creating a viewport used to look like, prior to 2.0
.
var viewport = ViewportMercator({
center: [0, 0],
zoom: 0,
dimensions: [600, 800]
});
The change was made to support the typical viewport
object from the new react-map-gl API changes.
FAQs
Utilities for perspective-enabled Web Mercator projections
The npm package viewport-mercator-project receives a total of 162,918 weekly downloads. As such, viewport-mercator-project popularity was classified as popular.
We found that viewport-mercator-project demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.