Proj4js ile Koordinat Dönüşümü
Proj4js, JavaScript uygulamalarında koordinat dönüşümü yapmak için kullanılan bir kütüphanedir. Tarayıcıda ve Node.js'de çalışır.
Kurulum
Shell
npm install proj4 CRS Tanımı Ekleme
JavaScript
import proj4 from 'proj4';
// CRS tanımını ekle
proj4.defs("EPSG:5255",
"+proj=tmerc +lat_0=0 +lon_0=33 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs"
); İhtiyacınız olan EPSG kodunu ana sayfada arayabilir ve Proj4js sekmesinden tanımı kopyalayabilirsiniz.
Koordinat Dönüşümü
JavaScript
// WGS84'ten EPSG:5255'e dönüştür
const result = proj4("EPSG:4326", "EPSG:5255", [33.0, 39.0]);
console.log(result); // [500000.00, 4317352.47]
// Ters dönüşüm
const wgs84 = proj4("EPSG:5255", "EPSG:4326", [500000, 4317352]);
console.log(wgs84); // [33.0, 38.99...] Leaflet ile Kullanım
JavaScript
import proj4 from 'proj4';
import 'proj4leaflet';
proj4.defs("EPSG:5255",
"+proj=tmerc +lat_0=0 +lon_0=33 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs"
);
const crs = new L.Proj.CRS('EPSG:5255',
proj4.defs('EPSG:5255'), {
origin: [0, 0],
resolutions: [8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
}
);
const map = L.map('map', { crs: crs }); OpenLayers ile Kullanım
JavaScript
import proj4 from 'proj4';
import { register } from 'ol/proj/proj4';
proj4.defs("EPSG:5255",
"+proj=tmerc +lat_0=0 +lon_0=33 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs"
);
register(proj4);
// Artık OpenLayers'da "EPSG:5255" kullanabilirsiniz Notlar
- Proj4js varsayılan olarak yalnızca
EPSG:4326veEPSG:3857tanımlarını içerir. - Diğer tüm CRS'ler için
proj4.defs()ile tanım eklemeniz gerekir. - Koordinat sırası
[longitude, latitude](x, y) şeklindedir.