Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Graticule Drawing for Grid Projections Other than EPSG4326 #15077

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dayanuyim
Copy link

Support for Graticule Drawing for Grid Projections Other than EPSG4326, with a additional option gridProjection

  • modified: src/ol/geom/flat/geodesic.js

    • patch meridian() and parallel() to allow to pass the sourceProjection, instead of hardcoded EPSG4326
  • modified: src/ol/layer/Graticule.js

    • add ctor options 'gridProjection', assign to property this.gridProjection_
    • use this.gridProjection_ to replace hardcoded EPSG4326
    • use gridProjection_.getExtent() to get the extent. (so users should define the extent for the gridProjection, if needed)
      (Originally, projection_.getWorldExtent() is used to do that, but it is only ok when gridProjection_ is EPSG4326)
  • TODO: suggest to change the variables' name from lonlat to xy

Use Example:

import {addProjection, Projection} from 'ol/proj';
import {register} from 'ol/proj/proj4';
import proj4 from 'proj4';

// ref: https://epsg.io/<NUMBER>.js
// ref: http://mutolisp.logdown.com/posts/207563-taiwan-geodetic-coordinate-system-conversion
 proj4.defs([
   ["EPSG:3828", "+title=TWD67 TM2 Taiwan +proj=tmerc +lat_0=0 +lon_0=121 +k=0.9999 +x_0=250000 +y_0=0 +ellps=aust_SA +towgs84=-752,-358,-179,-0.0000011698,0.0000018398,0.0000009822,0.00002329 +units=m +no_defs"],
 ]);

// reigster for openlayers
register(proj4);

export const TWD67 = new Projection({
  code: 'EPSG:3828',
  //center: 252551.25 2611288.37
  extent: [ 145616.57, 2419514.81, 356704.34, 2803869.61 ],
  worldExtent: [119.99, 21.87, 122.06, 25.34 ],
  units: 'm',
});

addProjection(TWD67);


//-----------------------------------------------------

import {Stroke} from 'ol/style';
import Graticule from 'ol/layer/Graticule';

const tm2_label_formatter = n => {
  const s = Math.floor(n).toString();
  return s.slice(0, -3) + ' ' + s.slice(-3);
}

const layer = new Graticule({
  strokeStyle: new Stroke({
    color: 'rgba(64,64,64,1)',
    width: 2,
    lineDash: [0.5, 4]
  }),
  lonLabelPosition: 0,
  latLabelPosition: 0,
  lonLabelStyle: lonLabelStyle(),
  latLabelStyle: latLabelStyle(),
  showLabels: true,
  maxLines: 20,
  wrapX: true,
  intervals: [100000, 10000, 1000, 100, 10, 1],
  lonLabelFormatter: tm2_label_formatter,
  latLabelFormatter: tm2_label_formatter,
  targetSize: 80,
  gridProjection: 'EPSG:3828',  //TWD67
});

- to draw graticule lines by specified gridProjection option, not default EPSG4326(WGS84).

modified:   src/ol/geom/flat/geodesic.js
  - patch meridian() and parallel() to allow to pass the sourceProjection, instead of hardcoded EPSG4326

modified:   src/ol/layer/Graticule.js
  - ctor options 'gridProjection', as property this.gridProjection_
  - use this.gridProjection_ to replace hardcoded EPSG4326
  - use gridProjection_.getExtent() to get the extent. (so users should define the extent for the gridProjection, if needed)
    (Originally, projection_.getWorldExtent() is used to do that, but it is only ok when gridProjection_ is EPSG4326)

- TODO suggest to change the variables' name from lonlat to xy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant