Skip to content

Commit

Permalink
Merge pull request #4 from SymbolixAU/wkt
Browse files Browse the repository at this point in the history
Wkt
  • Loading branch information
SymbolixAU authored Apr 7, 2018
2 parents 03df81d + baa6c45 commit 05081a4
Show file tree
Hide file tree
Showing 20 changed files with 849 additions and 39 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

export(geojson_sf)
export(geojson_sfc)
export(geojson_wkt)
importFrom(Rcpp,sourceCpp)
useDynLib(geojsonsf, .registration = TRUE)
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ rcpp_geojson_to_sf <- function(geojson) {
.Call(`_geojsonsf_rcpp_geojson_to_sf`, geojson)
}

rcpp_geojson_to_wkt <- function(geojson) {
.Call(`_geojsonsf_rcpp_geojson_to_wkt`, geojson)
}

rcpp_stream_in <- function(url) {
invisible(.Call(`_geojsonsf_rcpp_stream_in`, url))
}
Expand Down
10 changes: 10 additions & 0 deletions R/gsf_wkt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#' Geojson to WKT
#'
#' Converts GeoJSON to Well-Known Text
#'
#' @param geojson
#'
#' @export
geojson_wkt <- function(geojson) {
rcpp_geojson_to_wkt(geojson)
}
34 changes: 27 additions & 7 deletions R/scratch.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,30 @@
## - test erroneous inputs
## - no need to use / call / create 'properties' in construct_sfc

## ERRORS
# geojson_sf(1:5)
# geojson_sf("a")
# geojson_sf()
# geojson_sf(NULL)
# geojson_sf(NA)
# geojson_sf('{ "type" : "Point" }')
## - don't attach 'sfc' or 'sf' attributes



## wellknown can't handle properties?
# install.packages("wellknown")
#
# library(RCurl)
# myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
# geo <- readLines(url(myurl))
# geo <- paste0(geo, collapse = "")
#
# library(microbenchmark)
#
# str <- '{"type":"LineString","coordinates":[[0,0,10],[2,1,20],[4,2,30],[5,4,40]]}'
# wellknown::geojson2wkt(str)
#
# microbenchmark(
# geojsonsf = {
# geojson_wkt(geo)
# },
# wellknown = {
# wellknown::geojson2wkt(geo)
# },
# times = 2
# )

16 changes: 16 additions & 0 deletions inst/include/geojson_sfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,36 @@ using namespace rapidjson;
double get_lon(const Value& coord_array);
double get_lat(const Value& coord_array);

Rcpp::NumericVector parse_point(const Value& coord_array);

Rcpp::NumericVector parse_point(const Value& coord_array, Rcpp::NumericVector& bbox);

Rcpp::NumericMatrix parse_line(const Value& coord_array);

Rcpp::NumericMatrix parse_line(const Value& coord_array, Rcpp::NumericVector& bbox);

Rcpp::NumericVector get_point(const Value& point_array);

Rcpp::NumericVector get_point(const Value& point_array, Rcpp::NumericVector& bbox);

Rcpp::NumericMatrix get_multi_point(const Value& multi_point_array);

Rcpp::NumericMatrix get_multi_point(const Value& multi_point_array, Rcpp::NumericVector& bbox);

Rcpp::NumericMatrix get_line_string(const Value& line_array);

Rcpp::NumericMatrix get_line_string(const Value& line_array, Rcpp::NumericVector& bbox);

Rcpp::List get_multi_line_string(const Value& multi_line_array);

Rcpp::List get_multi_line_string(const Value& multi_line_array, Rcpp::NumericVector& bbox);

Rcpp::List get_polygon(const Value& polygon_array);

Rcpp::List get_polygon(const Value& polygon_array, Rcpp::NumericVector& bbox);

Rcpp::List get_multi_polygon(const Value& multi_polygon_array);

Rcpp::List get_multi_polygon(const Value& multi_polygon_array, Rcpp::NumericVector& bbox);


Expand Down
4 changes: 4 additions & 0 deletions inst/include/geojson_validate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ void safe_parse(Document& d, const char* geojson);

void validate_type(const Value& v, int& sfg_objects);

void validate_array(const Value& v);

void validate_array(const Value& v, int& sfg_objects);

void validate_features(const Value& v, int& sfg_objects);

void validate_feature(const Value& v, int& sfg_objects);
Expand Down
10 changes: 8 additions & 2 deletions inst/include/geojsonsf.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ namespace geojsonsf {

static const char* ARRAY_TYPES[] =
{ "Null", "False", "True", "Object", "Array", "String", "Number" };

}


#define UNKNOWN 0
#define POINT 1
#define MULTIPOINT 2
#define LINESTRING 3
#define MULTILINESTRING 4
#define POLYGON 5
#define MULTIPOLYGON 6
#define GEOMETRYCOLLECTION 7

#endif
14 changes: 14 additions & 0 deletions man/geojson_wkt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// rcpp_geojson_to_wkt
Rcpp::List rcpp_geojson_to_wkt(Rcpp::StringVector geojson);
RcppExport SEXP _geojsonsf_rcpp_geojson_to_wkt(SEXP geojsonSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::StringVector >::type geojson(geojsonSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_geojson_to_wkt(geojson));
return rcpp_result_gen;
END_RCPP
}
// rcpp_stream_in
void rcpp_stream_in(std::string url);
RcppExport SEXP _geojsonsf_rcpp_stream_in(SEXP urlSEXP) {
Expand All @@ -42,6 +53,7 @@ END_RCPP
static const R_CallMethodDef CallEntries[] = {
{"_geojsonsf_rcpp_geojson_to_sfc", (DL_FUNC) &_geojsonsf_rcpp_geojson_to_sfc, 1},
{"_geojsonsf_rcpp_geojson_to_sf", (DL_FUNC) &_geojsonsf_rcpp_geojson_to_sf, 1},
{"_geojsonsf_rcpp_geojson_to_wkt", (DL_FUNC) &_geojsonsf_rcpp_geojson_to_wkt, 1},
{"_geojsonsf_rcpp_stream_in", (DL_FUNC) &_geojsonsf_rcpp_stream_in, 1},
{NULL, NULL, 0}
};
Expand Down
15 changes: 13 additions & 2 deletions src/geojson_sfc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void fetch_geometries(Rcpp::List& sf, Rcpp::List& res, int& sfg_counter) {

std::string geom_attr;


for (Rcpp::List::iterator it = sf.begin(); it != sf.end(); it++) {

switch( TYPEOF(*it) ) {
Expand Down Expand Up @@ -129,6 +130,18 @@ void fetch_geometries(Rcpp::List& sf, Rcpp::List& res, int& sfg_counter) {
}
break;
}
case STRSXP: {
Rcpp::StringVector tmp = as<Rcpp::StringVector>(*it);
if(Rf_isNull(tmp.attr("class"))){
// TODO:
//handle missing geo_type in vector
Rcpp::stop("Geometry could not be determined");
} else {
res[sfg_counter] = tmp;
sfg_counter++;
}
break;
}
default: {
Rcpp::stop("Geometry could not be determined");
}
Expand All @@ -137,8 +150,6 @@ void fetch_geometries(Rcpp::List& sf, Rcpp::List& res, int& sfg_counter) {
}




Rcpp::List construct_sfc(int& sfg_objects,
Rcpp::List& sf,
Rcpp::NumericVector& bbox,
Expand Down
22 changes: 10 additions & 12 deletions src/geojson_sfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Rcpp.h>
#include "geojson_sfg.h"
#include "geojson_sfc.h"
#include "geojson_validate.h"

using namespace Rcpp;
using namespace rapidjson;
Expand All @@ -20,37 +21,36 @@ double get_lat(const Value& coord_array) {
}

Rcpp::NumericVector parse_point(const Value& coord_array, Rcpp::NumericVector& bbox) {
Rcpp::NumericVector point(2);
point[0] = get_lon(coord_array);
point[1] = get_lat(coord_array);
Rcpp::NumericVector point(2);
point[0] = get_lon(coord_array);
point[1] = get_lat(coord_array);
calculate_bbox(bbox, point);
return point;
}

Rcpp::NumericVector get_point(const Value& point_array, Rcpp::NumericVector& bbox) {
Rcpp::NumericVector point = parse_point(point_array, bbox);
point.attr("class") = sfg_attributes("POINT");
return point;
}

Rcpp::NumericMatrix parse_line(const Value& coord_array, Rcpp::NumericVector& bbox) {
size_t n = coord_array.Size();
Rcpp::NumericMatrix line_string(n, 2);
for (int i = 0; i < n; i++) {
validate_array(coord_array[i]);
line_string(i, _) = parse_point(coord_array[i], bbox);
}
return line_string;
}


Rcpp::NumericMatrix get_multi_point(const Value& multi_point_array, Rcpp::NumericVector& bbox) {
Rcpp::NumericMatrix multi_point = parse_line(multi_point_array, bbox);
multi_point.attr("class") = sfg_attributes("MULTIPOINT");
return multi_point;
}

Rcpp::NumericMatrix get_line_string(const Value& line_array, Rcpp::NumericVector& bbox) {
Rcpp::NumericMatrix line_string = parse_line(line_array, bbox);
line_string.attr("class") = sfg_attributes("LINESTRING");
return line_string;
}

Expand All @@ -59,9 +59,9 @@ Rcpp::List get_multi_line_string(const Value& multi_line_array, Rcpp::NumericVec
Rcpp::List multi_line(n);

for (int i = 0; i < n; i++) {
validate_array(multi_line_array[i]);
multi_line[i] = parse_line(multi_line_array[i], bbox);
}
multi_line.attr("class") = sfg_attributes("MULTILINESTRING");
return multi_line;
}

Expand All @@ -70,10 +70,9 @@ Rcpp::List get_polygon(const Value& polygon_array, Rcpp::NumericVector& bbox) {
Rcpp::List polygon(n);

for (int i = 0; i < n; i++) {
validate_array(polygon_array[i]);
polygon[i] = parse_line(polygon_array[i], bbox);
}

polygon.attr("class") = sfg_attributes("POLYGON");
return polygon;
}

Expand All @@ -82,18 +81,17 @@ Rcpp::List get_multi_polygon(const Value& multi_polygon_array, Rcpp::NumericVect
Rcpp::List multi_polygon(n);

for (int i = 0; i < n; i++) {

validate_array(multi_polygon_array[i]);
size_t np = multi_polygon_array[i].Size();
Rcpp::List polygon(np);
const Value& polygon_array = multi_polygon_array[i];

for (int j = 0; j < np; j++) {
validate_array(polygon_array[j]);
polygon[j] = parse_line(polygon_array[j], bbox);
}
multi_polygon[i] = polygon;
}

multi_polygon.attr("class") = sfg_attributes("MULTIPOLYGON");
return multi_polygon;
}

Expand Down
Loading

0 comments on commit 05081a4

Please sign in to comment.