Skip to content

Commit

Permalink
Merge pull request #198 from KNMI/add_polygon_labels
Browse files Browse the repository at this point in the history
Add polygon labels
  • Loading branch information
maartenplieger authored Apr 19, 2023
2 parents 27ab45d + f877702 commit 3bde2f8
Show file tree
Hide file tree
Showing 33 changed files with 1,253 additions and 226 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ USER root
LABEL maintainer="[email protected]"

# Version should be same as in Definitions.h
LABEL version="2.8.6"
LABEL version="2.8.7"

######### First stage (build) ############

Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
**Version 2.8.7 2023-04-19**
- GeoJSON with labels can now be displayed. See [FeatureInterval](doc/configuration/FeatureInterval.md) for details

**Version 2.8.6 2023-03-23**
- Added a configuration option to limit the number of deletions done at once, configurable with `cleanupsystemlimit="5"` in [Settings](doc/configuration/Settings.md).
- Added a configuration option to start a dryrun of cleanup, explaining which files would be deleted (but not actually deleting them), configurable with `enablecleanupsystem="dryrun"` in [Settings](doc/configuration/Settings.md)
Expand Down
16 changes: 14 additions & 2 deletions adagucserverEC/CCairoPlotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include "CCairoPlotter.h"
#ifdef ADAGUC_USE_CAIRO
//#define MEASURETIME
// #define MEASURETIME

#include "CStopWatch.h"
const char *CCairoPlotter::className = "CCairoPlotter";
Expand Down Expand Up @@ -239,7 +239,7 @@ int CCairoPlotter::renderFont(FT_Bitmap *bitmap, int left, int top) {
return 0;
}
int CCairoPlotter::initializeFreeType() {
// CDBDebug("initializeFreeType(%d)\n", library==NULL);
// CDBDebug("initializeFreeType(%d)\n", library == NULL);
if (library != NULL) {
CDBError("Freetype is already intialized");
return 1;
Expand Down Expand Up @@ -303,6 +303,7 @@ int CCairoPlotter::_drawFreeTypeText(int x, int y, int &w, int &h, float angle,
}
};
int error;
// CDBDebug("font: %s", this->fontLocation);

FT_GlyphSlot slot;
FT_Matrix matrix; /* transformation matrix */
Expand All @@ -320,13 +321,24 @@ int CCairoPlotter::_drawFreeTypeText(int x, int y, int &w, int &h, float angle,
/* start at (300,200) */
pen.x = x * 64;
pen.y = (my_target_height - y) * 64;
bool c3seen = false;
/* Using the 8859-15 standard */
for (n = 0; n < num_chars; n++) { /* set transformation */

FT_Set_Transform(face, &matrix, &pen); /* load glyph image into the slot (erase previous one) */

unsigned char characterToPrint = (unsigned char)text[n];
// Some tricks to handle UTF-8
if (characterToPrint == 194) continue;
if (c3seen) {
c3seen = false;
characterToPrint = characterToPrint + 0x40;
}
if (characterToPrint == 195) {
c3seen = true;
continue;
}

int glyphIndex = FT_Get_Char_Index(face, (characterToPrint));
error = FT_Load_Glyph(face, glyphIndex, FT_LOAD_RENDER);

Expand Down
69 changes: 69 additions & 0 deletions adagucserverEC/CColor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/******************************************************************************
*
* Project: ADAGUC Server
* Purpose: ADAGUC OGC Server
* Author: Maarten Plieger, plieger "at" knmi.nl
* Date: 2013-06-01
*
******************************************************************************
*
* Copyright 2013, Royal Netherlands Meteorological Institute (KNMI)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef CColor_H
#define CColor_H
#include <stdlib.h>
#include <CServerConfig_CPPXSD.h>
class CColor {
public:
unsigned char r, g, b, a;
CColor() {
r = 0;
g = 0;
b = 0;
a = 255;
}
CColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}
CColor(const char *color) { parse(color); }
/**
* color can have format #RRGGBB or #RRGGBBAA
*/
void parse(const char *color) {
size_t l = strlen(color);

if (l == 7 && color[0] == '#') {
r = CSERVER_HEXDIGIT_TO_DEC(color[1]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[2]);
g = CSERVER_HEXDIGIT_TO_DEC(color[3]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[4]);
b = CSERVER_HEXDIGIT_TO_DEC(color[5]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[6]);
a = 255;
} else if (l == 9 && color[0] == '#') {
r = CSERVER_HEXDIGIT_TO_DEC(color[1]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[2]);
g = CSERVER_HEXDIGIT_TO_DEC(color[3]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[4]);
b = CSERVER_HEXDIGIT_TO_DEC(color[5]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[6]);
a = CSERVER_HEXDIGIT_TO_DEC(color[7]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[8]);
} else {
r = 0;
g = 0;
b = 0;
a = 255;
}
}
};
#endif
6 changes: 4 additions & 2 deletions adagucserverEC/CDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,10 @@ double CDataSource::getScaling() {
double CDataSource::getContourScaling() {
if (this->getStyle() != NULL && this->getStyle()->styleConfig != NULL) {
if (this->getStyle()->styleConfig->RenderSettings.size() > 0) {
double scalecontours = this->getStyle()->styleConfig->RenderSettings[0]->attr.scalecontours.toDouble();
return scalecontours;
if (!this->getStyle()->styleConfig->RenderSettings[0]->attr.scalecontours.empty()) {
double scalecontours = this->getStyle()->styleConfig->RenderSettings[0]->attr.scalecontours.toDouble();
return scalecontours;
}
}
}
return 1;
Expand Down
36 changes: 36 additions & 0 deletions adagucserverEC/CDrawImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,42 @@ void CDrawImage::drawCenteredText(int x, int y, const char *fontfile, float size
}
}

void CDrawImage::drawCenteredTextNoOverlap(int x, int y, const char *fontFile, float size, float angle, int padding, const char *text, CColor color, bool noOverlap,
std::vector<CRectangleText> &rects) {
if (size <= 0) { // size 0 means do not draw label
return;
}

int w, h;
float radAngle = angle * M_PI / 180;
CRectangleText rect;
if (currentGraphicsRenderer == CDRAWIMAGERENDERER_CAIRO) {
CCairoPlotter *freeType = this->getCairoPlotter(fontFile, size, Geo->dWidth, Geo->dHeight, cairo->getByteBuffer());
freeType->setColor(color.r, color.g, color.b, color.a);
freeType->getTextSize(w, h, radAngle, text);
rect.init(x, y, (x + w), (y + h), angle, padding, text, fontFile, size, color);

} else {
// TODO GD renderer does not center text yet
char *_text = new char[strlen(text) + 1];
memcpy(_text, text, strlen(text) + 1);
int tcolor = getClosestGDColor(color.r, color.g, color.b);
if (_bEnableTrueColor) tcolor = -tcolor;
// Use the text size for angle 0 for detecting overlaps
gdImageStringFT(NULL, &brect[0], tcolor, (char *)fontFile, size, radAngle, x, y, (char *)_text);
rect.init(brect[0], brect[5], brect[4], brect[1], angle, padding, text, fontFile, size, color);
delete[] _text;
}
if (noOverlap) {
for (size_t j = 0; j < rects.size(); j++) {
if (rects[j].overlaps(rect)) {
return;
}
}
}
rects.push_back(rect);
}

void CDrawImage::drawText(int x, int y, const char *fontfile, float size, float angle, const char *text, CColor color) {

if (currentGraphicsRenderer == CDRAWIMAGERENDERER_CAIRO) {
Expand Down
47 changes: 6 additions & 41 deletions adagucserverEC/CDrawImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@
#include "CServerError.h"
#include "CServerConfig_CPPXSD.h"
#include <math.h>
// #include <png.h>
#include <gd.h>
#include "gdfontl.h"
#include "gdfonts.h"
#include "gdfontmb.h"
#include "CCairoPlotter.h"
#include "CColor.h"
#include "CRectangleText.h"

float convertValueToClass(float val, float interval);

Expand All @@ -63,47 +66,6 @@ class CLegend {
CT::string legendName;
};

class CColor {
public:
unsigned char r, g, b, a;
CColor() {
r = 0;
g = 0;
b = 0;
a = 255;
}
CColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}
CColor(const char *color) { parse(color); }
/**
* color can have format #RRGGBB or #RRGGBBAA
*/
void parse(const char *color) {
size_t l = strlen(color);

if (l == 7 && color[0] == '#') {
r = CSERVER_HEXDIGIT_TO_DEC(color[1]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[2]);
g = CSERVER_HEXDIGIT_TO_DEC(color[3]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[4]);
b = CSERVER_HEXDIGIT_TO_DEC(color[5]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[6]);
a = 255;
} else if (l == 9 && color[0] == '#') {
r = CSERVER_HEXDIGIT_TO_DEC(color[1]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[2]);
g = CSERVER_HEXDIGIT_TO_DEC(color[3]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[4]);
b = CSERVER_HEXDIGIT_TO_DEC(color[5]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[6]);
a = CSERVER_HEXDIGIT_TO_DEC(color[7]) * 16 + CSERVER_HEXDIGIT_TO_DEC(color[8]);
} else {
r = 0;
g = 0;
b = 0;
a = 255;
}
}
};

class CDrawImage {
public:
/*
Expand Down Expand Up @@ -172,6 +134,8 @@ class CDrawImage {
CGeoParams *Geo;
CDrawImage();
~CDrawImage();

public:
int createImage(int _dW, int _dH);
int createImage(CGeoParams *_Geo);
int createImage(const char *fn);
Expand All @@ -195,6 +159,7 @@ class CDrawImage {
void drawText(int x, int y, const char *fontfile, float size, float angle, const char *text, CColor fgcolor, CColor bgcolor);
void drawAnchoredText(int x, int y, const char *fontfile, float size, float angle, const char *text, CColor color, int anchor);
void drawCenteredText(int x, int y, const char *fontfile, float size, float angle, const char *text, CColor color);
void drawCenteredTextNoOverlap(int x, int y, const char *fontfile, float size, float angle, int padding, const char *text, CColor color, bool noOverlap, std::vector<CRectangleText> &rects);
int drawTextArea(int x, int y, const char *fontfile, float size, float angle, const char *text, CColor fgcolor, CColor bgcolor);

// void drawTextAngle(const char * text, size_t length,double angle,int x,int y,int color,int fontSize);
Expand Down
30 changes: 29 additions & 1 deletion adagucserverEC/CGeoJSONData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@

const char *Feature::className = "Feature";

CT::string FeatureProperty::toString() {
CT::string s;
if (type == typeInt) {
s.print("%ld", intVal);
} else if (type == typeStr) {
s.print("%s", pstr.c_str());
} else if (type == typeDouble) {
s.print("%f", dblVal);
} else {
s.print("NONE");
}
return s;
};

CT::string FeatureProperty::toString(const char *fmt) {
CT::string s;
if (type == typeInt) {
s.print(fmt, intVal);
} else if (type == typeStr) {
s.print(fmt, pstr.c_str());
} else if (type == typeDouble) {
s.print(fmt, dblVal);
} else {
s.print("NONE");
}
return s;
};

GeoPoint::GeoPoint(float lon, float lat) {
this->lat = lat;
this->lon = lon;
Expand Down Expand Up @@ -182,7 +210,7 @@ std::map<std::string, FeatureProperty *> *Feature::getFp() { return &fp; }

void Feature::addPoint(float lon, float lat) { points.push_back(GeoPoint(lon, lat)); }

//#define TESTIT
// #define TESTIT
#ifdef TESTIT
int main(int argc, char *argv[]) {
Feature poly("0001");
Expand Down
16 changes: 2 additions & 14 deletions adagucserverEC/CGeoJSONData.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,8 @@ class FeatureProperty {

CT::string getStringVal() { return pstr; }

CT::string toString() {
CT::string s;
if (type == typeInt) {
s.print("%ld", intVal);
} else if (type == typeStr) {
s.print("%s", pstr.c_str());
} else if (type == typeDouble) {
s.print("%f", dblVal);
} else {
s.print("NONE");
}
return s;
}
CT::string toString();
CT::string toString(const char *fmt);
};

// class FeatureProperties {
Expand Down Expand Up @@ -147,5 +136,4 @@ class Feature {
std::map<std::string, FeatureProperty *> *getFp();
bool hasHoles();
};

#endif
Loading

0 comments on commit 3bde2f8

Please sign in to comment.