Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
thejefflarson committed Jan 26, 2016
2 parents f48c899 + a21e565 commit c220a1a
Show file tree
Hide file tree
Showing 42 changed files with 893 additions and 1,191 deletions.
78 changes: 33 additions & 45 deletions src/bounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@
#include "memory.h"

// Extend the bounds to include the x, y point.
void
simplet_bounds_extend(simplet_bounds_t *bounds, double x, double y){
void simplet_bounds_extend(simplet_bounds_t *bounds, double x, double y) {
bounds->nw.x = fmin(x, bounds->nw.x);
bounds->nw.y = fmax(y, bounds->nw.y);
bounds->se.x = fmax(x, bounds->se.x);
bounds->se.y = fmin(y, bounds->se.y);
bounds->width = fabs(bounds->nw.x - bounds->se.x);
bounds->width = fabs(bounds->nw.x - bounds->se.x);
bounds->height = fabs(bounds->nw.y - bounds->se.y);
}

// Return a OGR_G_ConvexHull based on the bounds in a specified projection.
OGRGeometryH
simplet_bounds_to_ogr(simplet_bounds_t *bounds, OGRSpatialReferenceH proj) {
OGRGeometryH simplet_bounds_to_ogr(simplet_bounds_t *bounds,
OGRSpatialReferenceH proj) {
OGRGeometryH tmpLine;
if(!(tmpLine = OGR_G_CreateGeometry(wkbLineString)))
return NULL;
if (!(tmpLine = OGR_G_CreateGeometry(wkbLineString))) return NULL;

// Add all the points defining the bounds to the geometry
OGR_G_AddPoint_2D(tmpLine, bounds->nw.x, bounds->nw.y);
Expand All @@ -30,7 +28,7 @@ simplet_bounds_to_ogr(simplet_bounds_t *bounds, OGRSpatialReferenceH proj) {
OGRGeometryH tmpPoint = OGR_G_ForceToMultiPoint(tmpLine);
// Calculate the Convex Hull
OGRGeometryH ogrBounds;
if(!(ogrBounds = OGR_G_ConvexHull(tmpPoint))){
if (!(ogrBounds = OGR_G_ConvexHull(tmpPoint))) {
OGR_G_DestroyGeometry(tmpLine);
return NULL;
}
Expand All @@ -43,35 +41,32 @@ simplet_bounds_to_ogr(simplet_bounds_t *bounds, OGRSpatialReferenceH proj) {
}

// Test if one bounds intersects another.
int
simplet_bounds_intersects(simplet_bounds_t *bounds, simplet_bounds_t *obounds){
return !(bounds->nw.x > obounds->se.x || bounds->nw.y < obounds->se.y
|| bounds->se.x < obounds->nw.x || bounds->se.y > obounds->nw.y);
int simplet_bounds_intersects(simplet_bounds_t *bounds,
simplet_bounds_t *obounds) {
return !(bounds->nw.x > obounds->se.x || bounds->nw.y < obounds->se.y ||
bounds->se.x < obounds->nw.x || bounds->se.y > obounds->nw.y);
}

// Create a bounds from the Convex Hull of an OGR Geometry.
simplet_bounds_t*
simplet_bounds_from_ogr(OGRGeometryH geom){
simplet_bounds_t *simplet_bounds_from_ogr(OGRGeometryH geom) {
OGRGeometryH hull;

// Grab the Convex Hull
if(!(hull = OGR_G_ConvexHull(geom)))
return NULL;
if (!(hull = OGR_G_ConvexHull(geom))) return NULL;

// Create the bounds.
simplet_bounds_t *bounds;
if(!(bounds = simplet_bounds_new())){
if (!(bounds = simplet_bounds_new())) {
OGR_G_DestroyGeometry(hull);
return NULL;
}

// Extend the bounds by adding the points from the Convex Hull.
double x, y;
for(int i = 0; i < OGR_G_GetGeometryCount(hull); i++){
for (int i = 0; i < OGR_G_GetGeometryCount(hull); i++) {
OGRGeometryH subgeom = OGR_G_GetGeometryRef(hull, i);
if(subgeom == NULL)
continue;
for(int j = 0; j < OGR_G_GetPointCount(subgeom); j++){
if (subgeom == NULL) continue;
for (int j = 0; j < OGR_G_GetPointCount(subgeom); j++) {
OGR_G_GetPoint(subgeom, j, &x, &y, NULL);
simplet_bounds_extend(bounds, x, y);
}
Expand All @@ -82,17 +77,14 @@ simplet_bounds_from_ogr(OGRGeometryH geom){
}

// Free the memory associated with the bounds.
void
simplet_bounds_free(simplet_bounds_t *bounds){
if(simplet_release((simplet_retainable_t *)bounds) <= 0) free(bounds);
void simplet_bounds_free(simplet_bounds_t *bounds) {
if (simplet_release((simplet_retainable_t *)bounds) <= 0) free(bounds);
}

// Allocate and return a new simplet_bounds_t.
simplet_bounds_t*
simplet_bounds_new(){
simplet_bounds_t *simplet_bounds_new() {
simplet_bounds_t *bounds;
if(!(bounds = malloc(sizeof(*bounds))))
return NULL;
if (!(bounds = malloc(sizeof(*bounds)))) return NULL;

memset(bounds, 0, sizeof(*bounds));

Expand All @@ -107,23 +99,19 @@ simplet_bounds_new(){
}

// Convert a bounds to a Well Known Text string and store it in **wkt
simplet_status_t
simplet_bounds_to_wkt(simplet_bounds_t *bounds, char **wkt){
simplet_status_t simplet_bounds_to_wkt(simplet_bounds_t *bounds, char **wkt) {
int ret = asprintf(wkt, "POLYGON ((%f %f, %f %f, %f %f, %f %f, %f %f))",
bounds->se.x, bounds->nw.y,
bounds->se.x, bounds->se.y,
bounds->nw.x, bounds->se.y,
bounds->nw.x, bounds->nw.y,
bounds->se.x, bounds->nw.y);
if(ret > -1) return SIMPLET_OK;
bounds->se.x, bounds->nw.y, bounds->se.x, bounds->se.y,
bounds->nw.x, bounds->se.y, bounds->nw.x, bounds->nw.y,
bounds->se.x, bounds->nw.y);
if (ret > -1) return SIMPLET_OK;
return SIMPLET_ERR;
}

simplet_bounds_t*
simplet_bounds_buffer(simplet_bounds_t* bounds, double extend){
simplet_bounds_t* new;
if(!(new = simplet_bounds_new()))
return NULL;
simplet_bounds_t *simplet_bounds_buffer(simplet_bounds_t *bounds,
double extend) {
simplet_bounds_t *new;
if (!(new = simplet_bounds_new())) return NULL;

simplet_bounds_extend(new, bounds->nw.x - extend, bounds->nw.y + extend);
simplet_bounds_extend(new, bounds->se.x + extend, bounds->se.y - extend);
Expand All @@ -132,18 +120,18 @@ simplet_bounds_buffer(simplet_bounds_t* bounds, double extend){
}

// Reproject a simplet_bounds_t into a new projection and return a new copy.
simplet_bounds_t*
simplet_bounds_reproject(simplet_bounds_t* bounds, const char *from, const char *to){
simplet_bounds_t *simplet_bounds_reproject(simplet_bounds_t *bounds,
const char *from, const char *to) {
// Create a new spatial reference for `from`.
OGRSpatialReferenceH proj_from = OSRNewSpatialReference(NULL);
if(OSRSetFromUserInput(proj_from, from) != OGRERR_NONE) return NULL;
if (OSRSetFromUserInput(proj_from, from) != OGRERR_NONE) return NULL;

// Translate the bounds to an OGR object.
OGRGeometryH geom = simplet_bounds_to_ogr(bounds, proj_from);
OGRSpatialReferenceH proj_to = OSRNewSpatialReference(NULL);

// Create a new spatial reference for `to`
if(OSRSetFromUserInput(proj_to, to) != OGRERR_NONE) return NULL;
if (OSRSetFromUserInput(proj_to, to) != OGRERR_NONE) return NULL;
OGR_G_TransformTo(geom, proj_to);

// Create a bounds object from the OGR object.
Expand Down
31 changes: 13 additions & 18 deletions src/bounds.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,27 @@
extern "C" {
#endif

simplet_bounds_t*
simplet_bounds_new();
simplet_bounds_t *simplet_bounds_new();

void
simplet_bounds_extend(simplet_bounds_t *bounds, double x, double y);
void simplet_bounds_extend(simplet_bounds_t *bounds, double x, double y);

OGRGeometryH
simplet_bounds_to_ogr(simplet_bounds_t *bounds, OGRSpatialReferenceH proj);
OGRGeometryH simplet_bounds_to_ogr(simplet_bounds_t *bounds,
OGRSpatialReferenceH proj);

simplet_bounds_t*
simplet_bounds_from_ogr(OGRGeometryH geom);
simplet_bounds_t *simplet_bounds_from_ogr(OGRGeometryH geom);

void
simplet_bounds_free(simplet_bounds_t *bounds);
void simplet_bounds_free(simplet_bounds_t *bounds);

simplet_status_t
simplet_bounds_to_wkt(simplet_bounds_t *bounds, char **wkt);
simplet_status_t simplet_bounds_to_wkt(simplet_bounds_t *bounds, char **wkt);

simplet_bounds_t*
simplet_bounds_reproject(simplet_bounds_t* bounds, const char *from, const char *to);
simplet_bounds_t *simplet_bounds_reproject(simplet_bounds_t *bounds,
const char *from, const char *to);

int
simplet_bounds_intersects(simplet_bounds_t *bounds, simplet_bounds_t *obounds);
int simplet_bounds_intersects(simplet_bounds_t *bounds,
simplet_bounds_t *obounds);

simplet_bounds_t*
simplet_bounds_buffer(simplet_bounds_t* bounds, double extend);
simplet_bounds_t *simplet_bounds_buffer(simplet_bounds_t *bounds,
double extend);

#ifdef __cplusplus
}
Expand Down
20 changes: 11 additions & 9 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@
#include "error.h"

// Add a bit of debugging information to the error.
int
simplet_set_error(simplet_errorable_t *error, simplet_status_t status, const char *msg){
int simplet_set_error(simplet_errorable_t *error, simplet_status_t status,
const char *msg) {
int res = 1;
switch(status){
switch (status) {
case SIMPLET_ERR:
error->status = SIMPLET_ERR;
res = asprintf(&error->error_msg, "simple tiles error: %s", msg);
break;
case SIMPLET_OOM:
error->status = SIMPLET_OOM;
res = asprintf(&error->error_msg, "out of memory for allocation, %s", msg);
res =
asprintf(&error->error_msg, "out of memory for allocation, %s", msg);
break;
case SIMPLET_CAIRO_ERR:
error->status = SIMPLET_CAIRO_ERR;
res = asprintf(&error->error_msg, "cairo error: %s", msg);
break;
case SIMPLET_OGR_ERR:
error->status = SIMPLET_OGR_ERR;
res = asprintf(&error->error_msg, "OGR error: %s, %s", CPLGetLastErrorMsg(), msg);
res = asprintf(&error->error_msg, "OGR error: %s, %s",
CPLGetLastErrorMsg(), msg);
break;
case SIMPLET_GDAL_ERR:
error->status = SIMPLET_GDAL_ERR;
res = asprintf(&error->error_msg, "GDAL error: %s, %s", CPLGetLastErrorMsg(), msg);
res = asprintf(&error->error_msg, "GDAL error: %s, %s",
CPLGetLastErrorMsg(), msg);
break;
case SIMPLET_OK:
error->status = SIMPLET_OK;
Expand All @@ -35,9 +38,8 @@ simplet_set_error(simplet_errorable_t *error, simplet_status_t status, const cha
}

// Set error on an errorable.
simplet_status_t
simplet_error(simplet_errorable_t *errr, simplet_status_t err, const char *msg){
simplet_status_t simplet_error(simplet_errorable_t *errr, simplet_status_t err,
const char *msg) {
simplet_set_error(errr, err, msg);
return err;
}

14 changes: 7 additions & 7 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
extern "C" {
#endif

#define SIMPLET_ERROR_FUNC(type) \
static simplet_status_t \
set_error(simplet_##type *item, simplet_status_t status, const char *msg) { \
return simplet_error((simplet_errorable_t *)item, status, msg); \
}
#define SIMPLET_ERROR_FUNC(type) \
static simplet_status_t set_error( \
simplet_##type *item, simplet_status_t status, const char *msg) { \
return simplet_error((simplet_errorable_t *)item, status, msg); \
}

simplet_status_t
simplet_error(simplet_errorable_t *errr, simplet_status_t err, const char* msg);
simplet_status_t simplet_error(simplet_errorable_t *errr, simplet_status_t err,
const char *msg);

#ifdef __cplusplus
}
Expand Down
18 changes: 8 additions & 10 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@
static int initialized = 0;

// The atexit handler used to close all connections to open data stores
static void
cleanup(){
for(int i = 0; i < OGRGetOpenDSCount(); i++)
while(OGRGetOpenDS(i) && OGR_DS_GetRefCount(OGRGetOpenDS(i)))
static void cleanup() {
for (int i = 0; i < OGRGetOpenDSCount(); i++)
while (OGRGetOpenDS(i) && OGR_DS_GetRefCount(OGRGetOpenDS(i)))
OGRReleaseDataSource(OGRGetOpenDS(i));
assert(!OGRGetOpenDSCount());
OGRCleanupAll();
}

// Initialize libraries, register the atexit handler and set up error reporting.
void
simplet_init(){
if(initialized) return;
void simplet_init() {
if (initialized) return;
CPLSetConfigOption("OGR_ENABLE_PARTIAL_REPROJECTION", "ON");
#ifdef DEBUG
CPLSetConfigOption("CPL_DEBUG", "ON");
#endif
#ifdef DEBUG
CPLSetConfigOption("CPL_DEBUG", "ON");
#endif
OGRRegisterAll();
GDALAllRegister();
atexit(cleanup);
Expand Down
3 changes: 1 addition & 2 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
extern "C" {
#endif

void
simplet_init();
void simplet_init();

#ifdef __cplusplus
}
Expand Down
17 changes: 7 additions & 10 deletions src/layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@
SIMPLET_ERROR_FUNC(layer_t)

// Free a void pointer pointing to a layer instance.
void
simplet_layer_vfree(void *layer){
simplet_layer_t *cast_layer = (simplet_layer_t *) layer;
void simplet_layer_vfree(void *layer) {
simplet_layer_t *cast_layer = (simplet_layer_t *)layer;
if (cast_layer->type == SIMPLET_VECTOR) {
simplet_vector_layer_free((simplet_vector_layer_t *) cast_layer);
simplet_vector_layer_free((simplet_vector_layer_t *)cast_layer);
} else if (cast_layer->type == SIMPLET_RASTER) {
simplet_raster_layer_free((simplet_raster_layer_t *) cast_layer);
simplet_raster_layer_free((simplet_raster_layer_t *)cast_layer);
}
}

// Get the datasource string for this layer.
void
simplet_layer_get_source(simplet_layer_t *layer, char **source){
void simplet_layer_get_source(simplet_layer_t *layer, char **source) {
*source = simplet_copy_string(layer->source);
}

// Set a copy of this source as this layers datasource string.
void
simplet_layer_set_source(simplet_layer_t *layer, char *source){
void simplet_layer_set_source(simplet_layer_t *layer, char *source) {
char *src = simplet_copy_string(source);
if(!src) set_error(layer, SIMPLET_OOM, "out of memory setting source");
if (!src) set_error(layer, SIMPLET_OOM, "out of memory setting source");
layer->source = src;
}
10 changes: 3 additions & 7 deletions src/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
#include "text.h"
#include "user_data.h"


#ifdef __cplusplus
extern "C" {
#endif

void
simplet_layer_vfree(void *layer);
void simplet_layer_vfree(void *layer);

void
simplet_layer_get_source(simplet_layer_t *layer, char **source);
void simplet_layer_get_source(simplet_layer_t *layer, char **source);

void
simplet_layer_set_source(simplet_layer_t *layer, char *source);
void simplet_layer_set_source(simplet_layer_t *layer, char *source);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit c220a1a

Please sign in to comment.