You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Leaflet+Turf squareGrid异常:南半球象限网格计算返回0求助

Turf.js squareGrid Returns 0 Features for Southern Hemisphere Quadrants (Leaflet Integration)

Problem Analysis

Your issue comes down to a coordinate format mismatch between Leaflet and Turf.js, which throws off the bounding box calculation for southern hemisphere quadrants. Here's the breakdown:

  • Leaflet uses [latitude, longitude] order for coordinates in its GeoJSON output, but standard GeoJSON (and Turf.js) requires [longitude, latitude].
  • When you pass Leaflet's raw GeoJSON to turf.bbox, Turf misinterprets latitudes as longitudes and vice versa. For southern quadrants, this creates an invalid bounding box (e.g., where the minimum latitude is greater than the maximum latitude). Since turf.squareGrid can't generate grids in an invalid geographic range, it returns 0 features.
  • Northern quadrants worked by coincidence: their coordinate ranges didn't create an obviously invalid bbox when misread, but this was still incorrect behavior (you might have spotted subtle grid inaccuracies if you inspected them closely).

Solutions

You can fix this with either of these reliable approaches:

1. Manually Pass Valid Bounding Boxes

Since you know the exact bounds of each quadrant, skip GeoJSON conversion entirely and pass the correct bbox directly to turf.squareGrid. Turf expects bboxes in the format [minLon, minLat, maxLon, maxLat]:

// Quadrant C (blue): West Hemisphere Southern Hemisphere
const QGrid_C = turf.squareGrid([-180, -90, 0, 0], 500, { units: 'kilometers' });
// Quadrant D (yellow): East Hemisphere Southern Hemisphere
const QGrid_D = turf.squareGrid([0, -90, 180, 0], 500, { units: 'kilometers' });

2. Fix Leaflet's GeoJSON Coordinate Order

If you need dynamic GeoJSON conversion (e.g., for adjustable quadrants), use a helper function to swap latitude and longitude in Leaflet's output before sending it to Turf:

// Helper to convert Leaflet's [lat, lon] GeoJSON to standard [lon, lat]
function fixLeafletGeoJSON(leafletGeoJSON) {
  const corrected = JSON.parse(JSON.stringify(leafletGeoJSON));
  // Swap lat/lon for every coordinate in the polygon rings
  corrected.geometry.coordinates = corrected.geometry.coordinates.map(ring => 
    ring.map(coord => [coord[1], coord[0]])
  );
  return corrected;
}

// Apply the fix to southern quadrants (works for northern ones too!)
const quadrantCValidGeoJSON = fixLeafletGeoJSON(quadrantC.toGeoJSON());
const QGrid_C = turf.squareGrid(turf.bbox(quadrantCValidGeoJSON), 500, { units: 'kilometers' });

const quadrantDValidGeoJSON = fixLeafletGeoJSON(quadrantD.toGeoJSON());
const QGrid_D = turf.squareGrid(turf.bbox(quadrantDValidGeoJSON), 500, { units: 'kilometers' });

Verification

After applying either fix, QGrid_C and QGrid_D should return around 800 features each—matching the northern hemisphere results—since your quadrant division splits the globe into equal surface area sections.

内容的提问来源于stack exchange,提问作者Caius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:38:28