Skip to content

Commit

Permalink
和前端统一分辨率级别
Browse files Browse the repository at this point in the history
  • Loading branch information
zlzforever committed Apr 28, 2024
1 parent 065d900 commit c6eee62
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 59 deletions.
16 changes: 8 additions & 8 deletions src/Web/wmts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import WMTSTileGrid from 'ol/tilegrid/WMTS'
import proj4 from 'proj4'
import { register } from 'ol/proj/proj4'

const projection = getProjection('EPSG:3857')
const projection = getProjection('EPSG:4326')
const projectionExtent = projection.getExtent()
const size = getWidth(projectionExtent) / 256
const resolutions = new Array(22)
const matrixIds = new Array(22)
for (let z = 1; z < 22; ++z) {
for (let z = 0; z < 22; ++z) {
// generate resolutions and matrixIds arrays for this WMTS
resolutions[z - 1] = size / Math.pow(2, z)
matrixIds[z - 1] = z
resolutions[z] = size / Math.pow(2, z)
matrixIds[z] = z
}
debugger
let centerXY = [1388941.1429993785, 5139696.600312929]
let centerXY = [12.477070574987795, 41.85776175318455]

const map = new Map({
target: 'map',
layers: [
new TileLayer({
source: new WMTS({
url: 'http://localhost:8200/wmts',
layer: 'zserver:polygon_3857_shp',
matrixSet: 'EPSG:3857',
layer: 'zserver:layer_group',
matrixSet: 'EPSG:4326',
format: 'image/webp',
projection: projection,
tileGrid: new WMTSTileGrid({
Expand All @@ -45,7 +45,7 @@ const map = new Map({
view: new View({
projection: projection,
center: centerXY,
zoom: 16,
zoom: 12,
}),
})

Expand Down
80 changes: 40 additions & 40 deletions src/ZMap.TileGrid/GridSetFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,54 +179,54 @@ public static GridSet CreateGridSet(
int tileHeight,
bool yCoordinateFirst)
{
var extentWidth = extent.Width;
var extentHeight = extent.Height;

var resX = extentWidth / tileWidth;
var resY = extentHeight / tileHeight;

int tilesWide, tilesHigh;

if (resX <= resY)
{
// use one tile wide by N tiles high
tilesWide = 1;
tilesHigh = (int)Math.Round(resY / resX);
// previous resY was assuming 1 tile high, recompute with the actual number of tiles
// high
resY /= tilesHigh;
}
else
{
// use one tile high by N tiles wide
tilesHigh = 1;
tilesWide = (int)Math.Round(resX / resY);
// previous resX was assuming 1 tile wide, recompute with the actual number of tiles
// wide
resX /= tilesWide;
}

// the maximum of resX and resY is the one that adjusts better
var res = Math.Max(resX, resY);

var adjustedExtentWidth = tilesWide * tileWidth * res;
var adjustedExtentHeight = tilesHigh * tileHeight * res;

var adjExtent = new Envelope(extent.MinX, extent.MinX + adjustedExtentWidth,
extent.MaxY - adjustedExtentHeight, extent.MinY + adjustedExtentHeight);
// var extentWidth = extent.Width;
// var extentHeight = extent.Height;
//
// var resX = extentWidth / tileWidth;
// var resY = extentHeight / tileHeight;

// int tilesWide, tilesHigh;
//
// if (resX <= resY)
// {
// // use one tile wide by N tiles high
// tilesWide = 1;
// tilesHigh = (int)Math.Round(resY / resX);
// // previous resY was assuming 1 tile high, recompute with the actual number of tiles
// // high
// resY /= tilesHigh;
// }
// else
// {
// // use one tile high by N tiles wide
// tilesHigh = 1;
// tilesWide = (int)Math.Round(resX / resY);
// // previous resX was assuming 1 tile wide, recompute with the actual number of tiles
// // wide
// resX /= tilesWide;
// }
//
// // the maximum of resX and resY is the one that adjusts better
// var res = Math.Max(resX, resY);
//
// var adjustedExtentWidth = tilesWide * tileWidth * res;
// var adjustedExtentHeight = tilesHigh * tileHeight * res;
//
// var adjExtent = new Envelope(extent.MinX, extent.MinX + adjustedExtentWidth,
// extent.MaxY - adjustedExtentHeight, extent.MinY + adjustedExtentHeight);

var resolutions = new double[levels];
resolutions[0] = res;

for (var i = 1; i < levels; i++)
// resolutions[0] = res;
var size = extent.Width / tileWidth;
for (var i = 0; i < levels; i++)
{
resolutions[i] = resolutions[i - 1] / 2;
resolutions[i] = size / Math.Pow(2, i);
}

return CreateGridSet(
name,
srs,
adjExtent,
extent,
alignTopLeft,
resolutions,
null,
Expand Down
8 changes: 5 additions & 3 deletions src/ZMap/Infrastructure/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ ______ _____
/// <param name="layers"></param>
/// <param name="cqlFilter"></param>
/// <param name="format"></param>
/// <param name="tileMatrixSet"></param>
/// <param name="tileMatrix"></param>
/// <param name="tileRow"></param>
/// <param name="tileCol"></param>
/// <returns></returns>
public static string GetWmtsPath(string layers, string cqlFilter, string format, string tileMatrix, int tileRow,
public static string GetWmtsPath(string layers, string cqlFilter, string format, string tileMatrixSet,
string tileMatrix, int tileRow,
int tileCol)
{
var layerKey = layers.Replace(',', '_');
Expand All @@ -47,8 +49,8 @@ public static string GetWmtsPath(string layers, string cqlFilter, string format,
var imageExtension = GetImageExtension(format);
return Path.Combine(AppContext.BaseDirectory, "cache", "wmts",
string.IsNullOrEmpty(cqlFilterKey)
? $"{layerKey}/{tileMatrix}/{tileRow}/{tileCol}{imageExtension}"
: $"{layerKey}/{tileMatrix}/{tileRow}/{tileCol}_{cqlFilterKey}{imageExtension}");
? $"{layerKey}/{tileMatrixSet}/{tileMatrix}/{tileRow}/{tileCol}{imageExtension}"
: $"{layerKey}/{tileMatrixSet}/{tileMatrix}/{tileRow}/{tileCol}_{cqlFilterKey}{imageExtension}");
}

public static string GetWmtsKey(string layers, string tileMatrix, int tileRow,
Expand Down
4 changes: 3 additions & 1 deletion src/ZMap/Ogc/Wmts/WmtsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async ValueTask<MapResult> GetTileAsync(string layers, string styles,
$"Could not find tile matrix set {tileMatrixSet}");
}

var path = Utility.GetWmtsPath(layers, cqlFilter, format, tileMatrixSet, tileRow, tileCol);
var path = Utility.GetWmtsPath(layers, cqlFilter, format, tileMatrixSet, tileMatrix, tileRow, tileCol);
if (string.IsNullOrEmpty(path))
{
displayUrl = GetTileDisplayUrl(traceIdentifier, layers, styles, format, tileMatrixSet, tileMatrix,
Expand All @@ -66,6 +66,7 @@ public async ValueTask<MapResult> GetTileAsync(string layers, string styles,
"wmts key is empty");
}

#if !DEBUG
if (File.Exists(path))
{
if (EnvironmentVariables.EnableSensitiveDataLogging)
Expand All @@ -77,6 +78,7 @@ public async ValueTask<MapResult> GetTileAsync(string layers, string styles,

return new MapResult(File.OpenRead(path), null, null);
}
#endif

var folder = Path.GetDirectoryName(path);
if (folder != null && !Directory.Exists(folder))
Expand Down
3 changes: 3 additions & 0 deletions src/ZServer.API/Controllers/TestController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace ZServer.API.Controllers;
#if DEBUG
[ApiController]
Expand Down
16 changes: 11 additions & 5 deletions src/ZServer.API/Controllers/WMTSController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ public class WMTSController(IClusterClient clusterClient, ILogger<WMTSController
/// <param name="cqlFilter"></param>
/// <returns></returns>
[HttpGet]
public async Task GetAsync([Required] [FromQuery(Name = "layer")] string layers, string style,
[Required] string tileMatrix,
[Required] int tileRow, [Required] int tileCol, string format = "image/png",
string tileMatrixSet = "EPSG:4326", [FromQuery(Name = "CQL_FILTER")] string cqlFilter = null)
public async Task GetAsync([Required] [FromQuery(Name = "layer"), StringLength(100)] string layers,
[StringLength(100)] string style,
[Required, StringLength(50)] string tileMatrix, [Required] int tileRow, [Required] int tileCol,
string format = "image/png",
[Required, StringLength(50)] string tileMatrixSet = "EPSG:4326",
[FromQuery(Name = "CQL_FILTER"), StringLength(1000)]
string cqlFilter = null)
{
#if !DEBUG
// 使用相同的缓存路径
var path = Utility.GetWmtsPath(layers, cqlFilter, format, tileMatrixSet, tileRow, tileCol);
var path = Utility.GetWmtsPath(layers, cqlFilter, format, tileMatrixSet, tileMatrix, tileRow, tileCol);

if (System.IO.File.Exists(path))
{
if (EnvironmentVariables.EnableSensitiveDataLogging)
Expand All @@ -55,6 +60,7 @@ public async Task GetAsync([Required] [FromQuery(Name = "layer")] string layers,
await stream.CopyToAsync(HttpContext.Response.Body, (int)stream.Length);
return;
}
#endif

// 同一个 Grid 使用同一个对象进行管理, 保证缓存文件在同一个 Silo 目录下
var key = Utility.GetWmtsKey(layers, tileMatrixSet, tileRow, tileCol);
Expand Down
3 changes: 1 addition & 2 deletions src/ZServer.API/conf/zserver.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@
"layer_group": {
"resourceGroup": "zserver",
"layers": [
"polygon",
"polygon_3857"
"polygon"
]
}
},
Expand Down

0 comments on commit c6eee62

Please sign in to comment.