-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbgooglemapsmaker.cpp
More file actions
178 lines (145 loc) · 5.71 KB
/
bgooglemapsmaker.cpp
File metadata and controls
178 lines (145 loc) · 5.71 KB
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
#include "bgooglemapsmaker.h"
#include <Magick++.h>
#include <cmath>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
BGooglemapsMaker::BGooglemapsMaker(const string& fileName, const string& outputFolder, unsigned int minZoomlevel, unsigned int maxZoomlevel)
: m_outputMinZoom(minZoomlevel),
m_outputMaxZoom(maxZoomlevel),
m_outputTileSize(512)
{
m_fileName = fileName;
m_outputFolder = outputFolder;
}
BGooglemapsMaker::~BGooglemapsMaker()
{}
static inline std::string fillNumber(int number)
{
std::stringstream out;
out << setfill('0');
out << setw(3);
out << number;
return out.str();
}
bool BGooglemapsMaker::generateTilesMagick()
{
if (m_outputFolder.empty())
{
cerr << "No Outputfolder given" << endl;
return false;
}
// http://www.imagemagick.org/Magick++/Image.html#Image Manipulation Methods
Magick::Image image;
try {
cout << "Opening image: " << m_fileName << endl;
image.read(m_fileName);
cout << "done opening: " << m_fileName << endl;
cout << "width:" << image.columns() << " height:" << image.rows() << endl;
m_outputImageHeight = image.rows();
m_outputImageWidth = image.columns();
const double imageRatio = static_cast<double>(image.rows()) / static_cast<double>(image.columns());
for (unsigned int i = m_outputMaxZoom; i > 0; --i)
{
if (i < m_outputMaxZoom)
{
double scaledWidth = image.columns()/2.0;
int scaleToWidth = std::floor(scaledWidth);
int scaleToHeight = std::floor(scaledWidth*imageRatio);
cout << "starting scale to: " << scaleToWidth << "x" << scaleToHeight << " on level:"<< i<< endl;
image.sample(
Magick::Geometry(
scaleToWidth, scaleToHeight
)
);
cout << "done scaling on zoom level:" << i << endl;
}
double widthTileDoubleCount = static_cast<double>(image.columns()) / static_cast<double>(m_outputTileSize);
int widthTileCount = std::ceil(widthTileDoubleCount);
int heighTileCount = std::ceil((static_cast<double>(image.columns())*imageRatio) / static_cast<double>(m_outputTileSize));
cout << "Tiles on level:" << i << " x-tiles:" << widthTileCount << " y-tiles:" << heighTileCount << endl;
std::string zoomName = fillNumber(i);
const int maxNumber = heighTileCount * widthTileCount;
int run = 0;
int last = 0;
float tmp = 0;
float percent = static_cast<float>(maxNumber) / 10.0f;
for (int yTile = 0; yTile < heighTileCount; ++yTile)
{
std::string yTileName = fillNumber(yTile);
for (int xTile = 0; xTile < widthTileCount; ++xTile)
{
++run;
tmp = static_cast<int>(run / percent);
if (tmp != last)
{
cout << tmp * 10 << "% "<< run << "/" << maxNumber << " on zoomlevel:" << i << endl;
last = tmp;
}
Magick::Image tile(image);
tile.crop(
Magick::Geometry(
m_outputTileSize,
m_outputTileSize,
xTile * m_outputTileSize,
yTile * m_outputTileSize
)
);
if (tile.columns() != m_outputTileSize || tile.rows() != m_outputTileSize)
{
Magick::Image newtile(Magick::Geometry(m_outputTileSize, m_outputTileSize), Magick::Color(0, 0, 0, 1.0));
// newtile.floodFillTexture(Magick::Geometry(tile.columns(), tile.rows()), tile);
newtile.composite(tile, 0, 0);
tile = newtile;
}
std::string xTileName = fillNumber(xTile);
std::string saveName = m_outputFolder + '/' + m_outputPrefix + zoomName + xTileName + yTileName + ".png";
tile.write(saveName);
}
}
}
}
catch(Magick::Exception &error_)
{
cerr << "Caught exception: " << error_.what() << endl;
return false;
}
return true;
}
void BGooglemapsMaker::printParameters()
{
cerr << "m_outputImageHeight =" << m_outputImageHeight << endl;
cerr << "m_outputImageWidth =" << m_outputImageWidth << endl;
cerr << "constTileSize =" << m_outputTileSize << endl;
cerr << "maxZoomLevel =" << m_outputMaxZoom << endl;
// string outFileName = m_outputFolder + "/maps-data.js";
//
// FILE * outFile = fopen(outFileName.data(), "w");
// if (!outFile)
// {
// cerr << "WARNING: unabled to create" << m_outputFolder << "/" << "maps-data.js" << endl;
// exit(2);
// }
// fprintf(outFile, "var constTileSize = %d;\n", m_outputTileSize);
// fprintf(outFile, "var maxZoomLevel = %d;\n", m_outputMaxZoom);
// fprintf(outFile, "var constImageHeight = %d;\n", m_outputImageHeight);
// fprintf(outFile, "var constImageWidth = %d;\n", m_outputImageWidth);
// fclose(outFile);
}
void BGooglemapsMaker::setFileName(const string& fileName)
{
m_fileName = fileName;
}
void BGooglemapsMaker::setOutputFolder(const string& outputFolder)
{
m_outputFolder = outputFolder;
}
void BGooglemapsMaker::setMaxZoomLevel(unsigned int max)
{
m_outputMaxZoom = max;
}
void BGooglemapsMaker::setMinZoomLevel(unsigned int min)
{
m_outputMinZoom = min;
}