Skip to content

Commit

Permalink
load GeoJSON polygons and multi polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed Feb 1, 2020
1 parent c4b9f93 commit 5f454c4
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 15 deletions.
46 changes: 46 additions & 0 deletions src/data.cc
Expand Up @@ -14,6 +14,7 @@
#include "data.h"
#include "utils/fileutil.h"
#include "utils/csv.h"
#include "utils/geojson.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"
#include <assert.h>
Expand Down Expand Up @@ -184,6 +185,51 @@ ReturnCode data_load_strings(
return expr_to_strings(expr, values);
}

ReturnCode data_load_polys2_geojson(
const Expr* expr,
std::vector<Poly2>* data) {
if (!expr || !expr_is_value(expr)) {
return errorf(
ERROR,
"argument error; expected a filename, got: {}",
expr_inspect(expr));
}

const auto& path = expr_get_value(expr);

GeoJSONReader reader;
reader.on_polygons = [data] (const Poly3* polys, size_t poly_count) {
for (size_t i = 0; i < poly_count; ++i) {
data->emplace_back(poly3_to_poly2(polys[i]));
}

return OK;
};

return geojson_read_file(path, reader);
}

ReturnCode data_load_polys2(
const Expr* expr,
std::vector<Poly2>* data) {
if (!expr || !expr_is_list(expr) || !expr_get_list(expr)) {
return errorf(
ERROR,
"argument error; expected a list, got: {}",
expr_inspect(expr));
}

auto args = expr_get_list(expr);

if (args && expr_is_value_literal(args, "geojson")) {
return data_load_polys2_geojson(expr_next(args), data);
}

return err_invalid_value(expr_inspect(expr), {
"geojson"
});
}

ReturnCode data_load(
const Expr* expr,
std::vector<Measure>* values) {
Expand Down
4 changes: 4 additions & 0 deletions src/data.h
Expand Up @@ -52,6 +52,10 @@ ReturnCode data_load_strings(
const Expr* expr,
std::vector<std::string>* values);

ReturnCode data_load_polys2(
const Expr* expr,
std::vector<Poly2>* data);

ReturnCode data_load(
const Expr* expr,
std::vector<Measure>* values);
Expand Down
21 changes: 21 additions & 0 deletions src/graphics/geometry.cc
Expand Up @@ -48,5 +48,26 @@ std::ostream& operator <<(std::ostream& os, const Rectangle& r) {
return os;
}

PolyLine2 polyline3_to_polyline2(const PolyLine3& p) {
PolyLine2 p2;

for (const auto& x : p.vertices) {
p2.vertices.emplace_back(x);
}

return p2;
}

Poly2 poly3_to_poly2(const Poly3& p) {
Poly2 p2;
p2.boundary = polyline3_to_polyline2(p.boundary);

for (const auto& x : p.holes) {
p2.holes.emplace_back(polyline3_to_polyline2(x));
}

return p2;
}

} // namespace clip

29 changes: 29 additions & 0 deletions src/graphics/geometry.h
Expand Up @@ -16,6 +16,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <vector>
#include "vmath.h"

namespace clip {
Expand All @@ -33,5 +34,33 @@ struct Rectangle {

std::ostream& operator <<(std::ostream& os, const Rectangle& c);

struct PolyLine2 {
std::vector<vec2> vertices;
};

struct PolyLine3 {
std::vector<vec3> vertices;
};

/**
* - The poly lines are assumed to form closed 'rings'.
*/
struct Poly2 {
PolyLine2 boundary;
std::vector<PolyLine2> holes;
};

/**
* - The poly lines are assumed to form closed 'rings'.
*/
struct Poly3 {
PolyLine3 boundary;
std::vector<PolyLine3> holes;
};

PolyLine2 polyline3_to_polyline2(const PolyLine3& p);

Poly2 poly3_to_poly2(const Poly3& p);

} // namespace clip

0 comments on commit 5f454c4

Please sign in to comment.