前面有使用Node.js读取存储在PostGIS数据库中栅格影像发布xyz服务的文章,这篇是Apache版本。性能还需要优化直接上代码:
#ifndef TILE_DATABASE_HANDLER_H
#define TILE_DATABASE_HANDLER_H
#include <libpq-fe.h>
#include <stdlib.h>
typedef struct {
PGconn* conn;
} TileDatabaseHandler;
// Create and initialize the TileDatabaseHandler
TileDatabaseHandler* createTileDatabaseHandler();
// Destroy the TileDatabaseHandler and free its resources
void destroyTileDatabaseHandler(TileDatabaseHandler* handler);
// Get the tile data from the database given z, x, y coordinates
char* getTile(TileDatabaseHandler* handler, int z, int x, int y, size_t* dataLength);
#endif // TILE_DATABASE_HANDLER_H
#include "TileDatabaseHandler.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
// Function to create and initialize TileDatabaseHandler
TileDatabaseHandler* createTileDatabaseHandler() {
TileDatabaseHandler* handler = (TileDatabaseHandler*)malloc(sizeof(TileDatabaseHandler));
if (!handler) {
return NULL;
}
// Connection string
const char* conninfo = "host=192.168.101.201 port=4321 dbname=Tile user=postgres password=root";
// Establish connection to the database
handler->conn = PQconnectdb(conninfo);
// Check to ensure the connection was successful
if (PQstatus(handler->conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(handler->conn));
destroyTi