OpenLayers源码解析5 proj.js

本文深入探讨OpenLayers库中proj.js模块,主要关注其用于坐标转换的fromLonLat方法,该方法能够将经纬度坐标转换为特定坐标系的坐标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

proj.js

父类

主要功能

坐标系静态函数。

参数

方法

fromLonLat(coordinate, opt_projection)

返回值类型:{module:ol/coordinate-Coordinate}
作用:将经纬度坐标转换为目标坐标系坐标

coordinate module:ol/coordinate~Coordinate [经度,纬度]
projection ol/proj~ProjectionLike 目标坐标系,默认为球形墨卡托,即”EPSG:3857“
/**
 * @module ol/proj
 */

/**
 * The ol/proj module stores:
 * * a list of {@link module:ol/proj/Projection}
 * objects, one for each projection supported by the application
 * * a list of transform functions needed to convert coordinates in one projection
 * into another.
 *
 * The static functions are the methods used to maintain these.
 * Each transform function can handle not only simple coordinate pairs, but also
 * large arrays of coordinates such as vector geometries.
 *
 * When loaded, the library adds projection objects for EPSG:4326 (WGS84
 * geographic coordinates) and EPSG:3857 (Web or Spherical Mercator, as used
 * for example by Bing Maps or OpenStreetMap), together with the relevant
 * transform functions.
 *
 * Additional transforms may be added by using the http://proj4js.org/
 * library (version 2.2 or later). You can use the full build supplied by
 * Proj4js, or create a custom build to support those projections you need; see
 * the Proj4js website for how to do this. You also need the Proj4js definitions
 * for the required projections. These definitions can be obtained from
 * https://epsg.io/, and are a JS function, so can be loaded in a script
 * tag (as in the examples) or pasted into your application.
 *
 * After all required projection definitions are added to proj4's registry (by
 * using `proj4.defs()`), simply call `register(proj4)` from the `ol/proj/proj4`
 * package. Existing transforms are not changed by this function. See
 * examples/wms-image-custom-proj for an example of this.
 *
 * Additional projection definitions can be registered with `proj4.defs()` any
 * time. Just make sure to call `register(proj4)` again; for example, with user-supplied data where you don't
 * know in advance what projections are needed, you can initially load minimal
 * support and then load whichever are requested.
 *
 * Note that Proj4js does not support projection extents. If you want to add
 * one for creating default tile grids, you can add it after the Projection
 * object has been created with `setExtent`, for example,
 * `get('EPSG:1234').setExtent(extent)`.
 *
 * In addition to Proj4js support, any transform functions can be added with
 * {@link module:ol/proj.addCoordinateTransforms}. To use this, you must first create
 * a {@link module:ol/proj/Projection} object for the new projection and add it with
 * {@link module:ol/proj.addProjection}. You can then add the forward and inverse
 * functions with {@link module:ol/proj.addCoordinateTransforms}. See
 * examples/wms-custom-proj for an example of this.
 *
 * Note that if no transforms are needed and you only need to define the
 * projection, just add a {@link module:ol/proj/Projection} with
 * {@link module:ol/proj.addProjection}. See examples/wms-no-proj for an example of
 * this.
 */
import Projection from './proj/Projection.js';
import Units, {
   METERS_PER_UNIT} from './proj/Units.js';
import {
   
  PROJECTIONS as EPSG3857_PROJECTIONS,
  fromEPSG4326,
  toEPSG4326,
} from './proj/epsg3857.js';
import {
   PROJECTIONS as EPSG4326_PROJECTIONS} from './proj/epsg4326.js';
import {
   
  add as addProj,
  clear as clearProj,
  get as getProj,
} from './proj/projections.js';
import {
   
  add as addTransformFunc,
  clear as clearTransformFuncs,
  get as getTransformFunc,
} from './proj/transforms.js';
import {
   applyTransform, getWidth} from './extent.js';
import {
   clamp, modulo} from './math.js';
import {
   getDistance} from './sphere.js';
import {
   getWorldsAway} from './coordinate.js';

/**
 * A projection as {@link module:ol/proj/Projection}, SRS identifier
 * string or undefined.
 * @typedef {Projection|string|undefined} ProjectionLike
 * @api
 */

/**
 * A transform function accepts an array of input coordinate values, an optional
 * output array, and an optional dimension (default should be 2).  The function
 * transforms the input coordinate values, populates the output array, and
 * returns the output array.
 *
 * @typedef {function(Array<number>, Array<number>=, number=): Array<number>} TransformFunction
 * @api
 */

export {
   METERS_PER_UNIT};

export {
   Projection};

/**
 * @param {Array<number>} input Input coordinate array.
 * @param {Array<number>} [opt_output] Output array of coordinate values.
 * @param {number} [opt_dimension] Dimension.
 * @return {Array<number>} Output coordinate array (new array, same coordinate
 *     values).
 */
export function cloneTransform(input, opt_output, opt_dimension) {
   
  let output;
  if (opt_output !== undefined) {
   
    for (let i = 0, ii = input.length; i < ii; ++i) {
   
      opt_output[i] = input[i];
    }
    output = opt_output;
  } else {
   
    output = input.slice();
  }
  return output;
}

/**
 * @param {Array<number>} input Input coordinate array.
 * @param {Array<number>} [opt_output] Output array of coordinate values.
 * @param {number} [opt_dimension] Dimension.
 * @return {Array<number>} Input coordinate array (same array as input).
 */
export function identityTransform(input, opt_output, opt_dimension) {
   
  if (opt_output !== undefined && input !== opt_output) {
   
    for (let i = 0, ii = input.length; i < ii; ++i) {
   
      opt_output[i] = input[i];
    }
    input = opt_output;
  }
  return input;
}

/**
 * Add a Projection object to the list of supported projections that can be
 * looked up by their code.
 *
 * @param {Projection} projection Projection instance.
 * @api
 */
export function addProjection(projection) {
   
  addProj(projection.getCode(), projection);
  addTransformFunc(projection, projection, cloneTransform);
}

/**
 * @param {Array<Projection>} projections Projections.
 */
export function addProjections(projections) {
   
  projections.forEach(addProjection);
}

/**
 * Fetches a Projection object for the code specified.
 *
 * @param {ProjectionLike} projectionLike Either a code string which is
 *     a combination of authority and identifier such as "EPSG:4326", or an
 *     existing projection object, or undefined.
 * @return {Projection} Projection object, or null if not in list.
 * @api
 */
export function get(projectionLike) {
   
  return typeof projectionLike 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值