OpenLayers 4集成D3时Mercator投影初始化疑问:scale设1、translate设[0,0]原因
scale(1) and translate([0, 0]) When Initializing D3's Mercator Projection for OpenLayers 4 Integration? Great question—this is a common, clever trick when syncing D3 with mapping libraries like OpenLayers, so let’s break it down in plain terms:
Start with a "blank slate" projection
D3’sd3.geo.mercator()comes with built-in default scale and translate values, tuned to display the entire world in a standard browser window. But when integrating with OpenLayers, we don’t want D3 using its own default spatial rules—we need it to match OpenLayers’ view exactly (including zoom level, map center, and container size). Settingscale(1)andtranslate([0, 0])resets the D3 projection to a raw, unmodified state: it only handles the core geographic-to-Mercator coordinate conversion, no extra scaling or shifting.Sync perfectly with OpenLayers’ coordinate system
OpenLayers manages its own projection and view transformations (like turning geographic coordinates into pixel positions in the map container). By starting with a neutral D3 projection, you can later calculate the exactscaleandtranslatevalues needed to mirror OpenLayers’ current view state. For example, you might use OpenLayers’getResolution(),getCenter(), or projection conversion tools to compute how D3 should scale and shift its output to overlay flawlessly on the OL map.Steer clear of conflicting defaults
If you skipped this step and used D3’s out-of-the-box settings, your D3-drawn elements would almost certainly be misaligned—either zoomed too far in/out, shifted off the map, or both. The neutral base ensures you’re building the projection alignment from a clean start, making the math for syncing with OpenLayers way simpler and more reliable.
Content of the question comes from stack exchange, question author: sknight

