1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
#!/usr/bin/env python3
#
# -*- coding: utf-8 -*-
# ******************************************************************************
# $Id$
#
# Name: wcs_virtds_params.py
# Project: GDAL Python Interface
# Purpose: Generates MapServer WCS layer definition from a tileindex with mixed SRS
# Author: Even Rouault, <even dot rouault at spatialys.com>
#
# ******************************************************************************
# Copyright (c) 2013, Even Rouault <even dot rouault at spatialys.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ******************************************************************************
import os
import sys
from osgeo import gdal, ogr, osr
def Usage():
print(
"Usage: wcs_virtds_params.py [-lyr_name name] [-tileindex field_name] [-t_srs srsdef] -src_srs_name field_name ogr_ds_tileindex"
)
return 2
def main(argv=sys.argv):
argv = ogr.GeneralCmdLineProcessor(argv)
ogr_ds_name = None
lyr_name = None
tileitem = "location"
tilesrs = None
srsname = None
nArgc = len(argv)
iArg = 1
while iArg < nArgc:
if argv[iArg] == "-lyr_name" and iArg < nArgc - 1:
iArg = iArg + 1
lyr_name = argv[iArg]
elif argv[iArg] == "-tileindex" and iArg < nArgc - 1:
iArg = iArg + 1
tileitem = argv[iArg]
elif argv[iArg] == "-src_srs_name" and iArg < nArgc - 1:
iArg = iArg + 1
tilesrs = argv[iArg]
elif argv[iArg] == "-t_srs" and iArg < nArgc - 1:
iArg = iArg + 1
srsname = argv[iArg]
elif argv[iArg][0] == "-":
return Usage()
elif ogr_ds_name is None:
ogr_ds_name = argv[iArg]
iArg = iArg + 1
if ogr_ds_name is None or tilesrs is None:
return Usage()
ogr_ds = ogr.Open(ogr_ds_name)
if ogr_ds is None:
raise Exception("cannot open %s" % ogr_ds_name)
if ogr_ds.GetLayerCount() == 1:
lyr = ogr_ds.GetLayer(0)
elif lyr_name is None:
raise Exception("-lyr_name should be specified")
else:
lyr = ogr_ds.GetLayerByName(lyr_name)
if lyr.GetLayerDefn().GetFieldIndex(tileitem) < 0:
raise Exception("%s field cannot be found in layer definition" % tileitem)
if lyr.GetLayerDefn().GetFieldIndex(tilesrs) < 0:
raise Exception("%s field cannot be found in layer definition" % tilesrs)
lyr_srs = lyr.GetSpatialRef()
if srsname is not None:
srs = osr.SpatialReference()
if srs.SetFromUserInput(srsname) != 0:
raise Exception("invalid value for -t_srs : %s" % srsname)
# Sanity check
if lyr_srs is not None:
lyr_srs_proj4 = lyr_srs.ExportToProj4()
lyr_srs = osr.SpatialReference()
lyr_srs.SetFromUserInput(lyr_srs_proj4)
lyr_srs_proj4 = lyr_srs.ExportToProj4()
srs_proj4 = srs.ExportToProj4()
if lyr_srs_proj4 != srs_proj4:
raise Exception(
"-t_srs overrides the layer SRS in an incompatible way : (%s, %s)"
% (srs_proj4, lyr_srs_proj4)
)
else:
srs = lyr_srs
if srs is None:
raise Exception("cannot fetch source SRS")
srs.AutoIdentifyEPSG()
authority_name = srs.GetAuthorityName(None)
authority_code = srs.GetAuthorityCode(None)
dst_wkt = srs.ExportToWkt()
if authority_name != "EPSG" or authority_code is None:
raise Exception("cannot fetch source SRS as EPSG:XXXX code : %s" % dst_wkt)
counter = 0
xres = 0
yres = 0
while True:
feat = lyr.GetNextFeature()
if feat is None:
break
# feat.DumpReadable()
gdal_ds_name = feat.GetField(tileitem)
if not os.path.isabs(gdal_ds_name):
gdal_ds_name = os.path.join(os.path.dirname(ogr_ds_name), gdal_ds_name)
gdal_ds = gdal.Open(gdal_ds_name)
if gdal_ds is None:
raise Exception("cannot open %s" % gdal_ds_name)
warped_vrt_ds = gdal.AutoCreateWarpedVRT(gdal_ds, None, dst_wkt)
if warped_vrt_ds is None:
raise Exception("cannot warp %s to %s" % (gdal_ds_name, dst_wkt))
gt = warped_vrt_ds.GetGeoTransform()
xres += gt[1]
yres += gt[5]
warped_vrt_ds = None
counter = counter + 1
if counter == 0:
raise Exception("tileindex is empty")
xres /= counter
yres /= counter
(xmin, xmax, ymin, ymax) = lyr.GetExtent()
xsize = (int)((xmax - xmin) / xres + 0.5)
ysize = (int)((ymax - ymin) / abs(yres) + 0.5)
layername = lyr.GetName()
if ogr_ds.GetDriver().GetName() != "ESRI Shapefile":
print(
"""LAYER
NAME "%s_tileindex"
TYPE POLYGON
STATUS OFF
CONNECTIONTYPE OGR
CONNECTION "%s,%s"
PROJECTION
"+init=epsg:%s"
END
END"""
% (layername, ogr_ds_name, lyr.GetName(), authority_code)
)
print("")
tileindex = "%s_tileindex" % layername
else:
tileindex = ogr_ds_name
print(
"""LAYER
NAME "%s"
TYPE RASTER
STATUS ON
TILEINDEX "%s"
TILEITEM "%s"
TILESRS "%s"
PROJECTION
"+init=epsg:%s"
END
METADATA
"wcs_label" "%s"
"wcs_rangeset_name" "Range 1" ### required to support DescribeCoverage request
"wcs_rangeset_label" "My Label" ### required to support DescribeCoverage request
"wcs_extent" "%f %f %f %f"
"wcs_size" "%d %d"
"wcs_resolution" "%f %f"
END
END"""
% (
layername,
tileindex,
tileitem,
tilesrs,
authority_code,
layername,
xmin,
ymin,
xmax,
ymax,
xsize,
ysize,
xres,
abs(yres),
)
)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))
|