www.pudn.com > dstile-0.2.rar > main.cpp


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#include "exceptions.h"
#include "syskit.h"
#include "giskit.h"
#include "TileAccessor.h"
#include "TileProcessor.h"
#include "CSTrans.h"
#include "Warper.h"
#include "Overviews.h"
#include "Tiler.h"

int TileMain(vector& args) {
    GDALAllRegister();
    GDALSetCacheMax(128000000);    
    TileAccessor ta;
    TileProcessor tp;
    CSTrans cst;
    Warper wa;
    Tiler dst;
    Overviews ov;
    bool overviews = false;
    string srcProj;
    int level = -1;
    int j;
//    vector ofp;

    if (args.size() == 1) {
	fprintf(stderr, "Usage: %s tile [OPTION]... TILES_PATH IMAGE [IMAGE]...", args[0].c_str());
	fprintf(stderr, "  --l0TilesCount x y     Set level 0 tiles count\n");
	fprintf(stderr, "  --srcProj proj         Enforce source projection\n");
	fprintf(stderr, "  --level n              Enforce level\n");
	fprintf(stderr, "  --overviews            Build overviews after tiling\n");
	fprintf(stderr, "  --lztsd                Level 0 Tile Size in Degrees\n");
	fprintf(stderr, "  --wwcache              WWCache Format Tiles Generated\n");
	return 0;
    }

    ta.SetExt(".jpg");
//    ofp.push_back("WORLDFILE=ON");
    ta.SetOutputFormat("JPEG");
    ta.SetWWCache(false);
    
    tp.SetMinDataVal(10);

    dst.SetTileAccessor(&ta);
    dst.SetTileProcessor(&tp);
    dst.SetCSTrans(&cst);
    dst.SetWarper(&wa);

    dst.SetDstProj("EPSG:4326");
    dst.SetDstExt(-180.0, 90.0, 180.0, -270.0);
    dst.SetTileSize(512, 512);

    j = 0;
    for (int i = 1; i < args.size(); ++i) {
	if (strcasecmp(args[i].c_str(), "--srcProj") == 0) {
	    srcProj = args[++i];
	} else if (strcasecmp(args[i].c_str(), "--dstProj") == 0) {
	    dst.SetDstProj(args[++i]);
	} else if (strcasecmp(args[i].c_str(), "--dstExt") == 0) {
	    dst.SetDstExt(atof(args[++i].c_str()), atof(args[++i].c_str()), atof(args[++i].c_str()), atof(args[++i].c_str()));
	} else if (strcasecmp(args[i].c_str(), "--l0TilesCount") == 0) {
		int locx = atoi(args[++i].c_str());
		int locy = atoi(args[++i].c_str());
		dst.SetL0TilesCount(locx,locy);
		ta.SetL0TilesCount(locx,locy);
		fprintf(stderr, "lztsd set to %d %d\n",360/locx,360/locy);
	}
	else if (strcasecmp(args[i].c_str(), "--lztsd") == 0) {
		int lztsd = atoi(args[++i].c_str());
	        dst.SetL0TilesCount(360/lztsd,360/lztsd);
		ta.SetL0TilesCount(360/lztsd,360/lztsd);
		fprintf(stderr, "lztsd set to %d\n",lztsd);
	}
	else if (strcasecmp(args[i].c_str(), "--wwcache") == 0) {
	        ta.SetWWCache(true);
		fprintf(stderr, "Tile Mode set to WW Cache");
	}
	else if (strcasecmp(args[i].c_str(), "--overviews") == 0) {
	    if (!overviews) {
		overviews = true;
		ov.SetTileAccessor(&ta);
		ov.SetTileProcessor(&tp);
		dst.AttachOverviews(&ov);
	    }
	} else if (strcasecmp(args[i].c_str(), "--level") == 0) {
	    level = atoi(args[++i].c_str());
	} else {
	    if (j == 0) ta.SetPath(args[i]);
	    else {
		dst.OpenSrc(args[i]);
		if (srcProj.size()) dst.SetSrcProj(srcProj);
		dst.BeginTiling();
		if (level < 0) dst.Tile(static_cast(ceil(dst.EstimateLevel() - 0.4)));
		else dst.Tile(-1);
		dst.EndTiling();
		dst.CloseSrc();
	    }
	    ++j;
	}
    }
    if ((j >= 2) && overviews) ov.BuildOverviews();
    return 0;
}

int OverviewsMain(vector& args) {
    GDALAllRegister();
    GDALSetCacheMax(128000000);    
}

int PrepMain(vector& args) {
    GDALAllRegister();
    TileProcessor tp;
    GDALDataset *ds = 0;
    int blackOut = 0;
    int blackIn = 0;
    int whiteOut = 255;
    int whiteIn = 255;
    int keepBlack = 0;
    int whiteToBlack = -1;

    for (int i = 1; i < args.size(); ++i) {
	if (strcasecmp(args[i].c_str(), "--blackOut") == 0) {
	    blackOut = atoi(args[++i].c_str());
	} else if (strcasecmp(args[i].c_str(), "--blackIn") == 0) {
	    blackIn = atoi(args[++i].c_str());
	} else if (strcasecmp(args[i].c_str(), "--whiteOut") == 0) {
	    whiteOut = atoi(args[++i].c_str());
	} else if (strcasecmp(args[i].c_str(), "--whiteIn") == 0) {
	    whiteIn = atoi(args[++i].c_str());
	} else if (strcasecmp(args[i].c_str(), "--keepBlack") == 0) {
	    keepBlack = atoi(args[++i].c_str());
	} else if (strcasecmp(args[i].c_str(), "--whiteToBlack") == 0) {
	    whiteToBlack = atoi(args[++i].c_str());
	} else {
	    ds = reinterpret_cast(GDALOpen(args[i].c_str(), GA_Update));
	    if (!ds) throw GDALException("Unable to open '%s'.", args[i].c_str());
	    try {
		tp.LinearTransform(ds, ds, blackOut, blackIn, whiteOut, whiteIn, keepBlack, whiteToBlack);
	    }
	    catch (...) {
		if (ds) GDALClose(ds);
	    }
	    GDALClose(ds);
	}
    }
    
	

    return 0;
}

int PackMain(vector& args) {
    return 0;
}

int main(int argc, char *argv[]) {
    vector args;
    int ret;

    try {
	for (int i = 0; i < argc; ++i) args.push_back(argv[i]);
	if (args.size() < 2) {
	    fprintf(stderr, "RUN: %s command ...\n", argv[0]);
	    fprintf(stderr, "  tile\n");
	    fprintf(stderr, "  overviews\n");
	    fprintf(stderr, "  prep\n");
	    fprintf(stderr, "  pack\n");
	    return 1;
	}
	if (!strcasecmp(args[1].c_str(), "tile")) {
	    args.erase(args.begin() + 1);
	    ret = TileMain(args);
	} else if (!strcasecmp(args[1].c_str(), "overviews")) {
	    args.erase(args.begin() + 1);
    	    ret = OverviewsMain(args);
	} else if (!strcasecmp(args[1].c_str(), "prep")) {
	    args.erase(args.begin() + 1);
	    ret = PrepMain(args);
	} else if (!strcasecmp(args[1].c_str(), "pack")) {
	    args.erase(args.begin() + 1);
	    ret = PackMain(args);
	} else {
	    fprintf(stderr, "Unknown command '%s'.\n", args[1].c_str());
	    ret = 1;
	}
    }
    catch (Exception &e) {
	e.Print();
	return 1;
    }
    
    return ret;
}