Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geojson #9

Merged
merged 27 commits into from
Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: geojsonsf
Type: Package
Title: GeoJSON to Simple Feature Converter
Version: 0.3.0003
Version: 0.3.0004
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre"))
)
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ S3method(geojson_sf,default)
S3method(geojson_sfc,character)
S3method(geojson_sfc,connection)
S3method(geojson_sfc,default)
S3method(sf_geojson,sf)
S3method(sf_geojson,sfc)
S3method(sf_geojson,sfg)
export(geojson_sf)
export(geojson_sfc)
export(geojson_wkt)
export(sf_geojson)
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 @@ -21,3 +21,7 @@ rcpp_read_sf_file <- function(file) {
.Call(`_geojsonsf_rcpp_read_sf_file`, file)
}

rcpp_sf_to_geojson <- function(sf, atomise) {
.Call(`_geojsonsf_rcpp_sf_to_geojson`, sf, atomise)
}

30 changes: 30 additions & 0 deletions R/geojson_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ geojson_sf.connection <- function(geojson) geojson_sf(read_url(geojson))
#' @export
geojson_sf.default <- function(geojson) rcpp_geojson_to_sf(geojson)


## TODO:
## - atomise - logical?
## -- Return a JSON array of objects
## -- Return an R Vector of objects?

#' sf to GeoJSON
#'
#' Converts `sf`, `sfc` and `sfg` objects to GeoJSON
#'
#' @param sf simple feature object
#' @param atomise logical
#'
#' @export
sf_geojson <- function(sf, atomise = FALSE) UseMethod("sf_geojson")

#' @export
sf_geojson.sf <- function(sf, atomise) rcpp_sf_to_geojson(sf, atomise)

#' @export
sf_geojson.sfc <- function(sf, atomise) rcpp_sf_to_geojson(sf, atomise)

#' @export
sf_geojson.sfg <- function(sf, atomise) rcpp_sf_to_geojson(sf, atomise)

sf_geojson.default <- function(sf, atomise) stop("Expected an sf object")




is_url <- function(geojson) grepl("^https?://", geojson, useBytes=TRUE)

read_url <- function(con) {
Expand Down
129 changes: 129 additions & 0 deletions R/scratch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
## sf to GeoJSON

## stream the data.frame, row by row.
##

# js <- '{"type" : "Point", "coordinates" : [0, 0]}'
# sf <- geojson_sf(js)
# sf
# sf_geojson(sf)
#
# js <- '{"type" : "MultiPoint", "coordinates" : [ [0, 0], [1, 1] ]}'
# sf <- geojson_sf(js)
# sf
# sf_geojson(sf)
#
# js <- '{"type" : "MultiLineString", "coordinates" : [ [ [0, 0], [1, 1] ] ]}'
# sf <- geojson_sf(js)
# sf
# sf_geojson(sf)
#
# js <- '{"type" : "Polygon", "coordinates" : [ [ [0, 0], [1, 1] ] ]}'
# sf <- geojson_sf(js)
# sf
# sf_geojson(sf)
#
# js <- '[{"type" : "Polygon", "coordinates" : [ [ [0, 0], [1, 1] ] ]},
# {"type" : "MultiLineString", "coordinates" : [ [ [0, 0], [1, 1] ], [[3,3],[4,4]] ]}]'
# sf <- geojson_sf(js)
# sf
# sf_geojson(sf)

# js <- '{
# "type": "GeometryCollection", "geometries": [
# {"type": "Point", "coordinates": [100.0, 0.0]},
# {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]},
# {"type" : "MultiPoint", "coordinates" : [[0,0], [1,1], [2,2]]}
# ]}'
# sf <- geojson_sf(js)
# j <- sf_geojson(sf)
#
# j
# gsub(" |\\n|\\r","",js)


# js <- '[{"type" : "Polygon", "coordinates" : [ [ [0, 0], [1, 1] ] ]},
# {"type" : "MultiLineString", "coordinates" : [ [ [0, 0], [1, 1] ] ]},
# {
# "type": "GeometryCollection", "geometries": [
# {"type": "Point", "coordinates": [100.0, 0.0]},
# {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]},
# {"type" : "MultiPoint", "coordinates" : [[0,0], [1,1], [2,2]]}
# ]}
# ]'
# sf <- geojson_sf(js)
# sf
# sf_geojson(sf)

# js <- '[
# {
# "type": "Feature",
# "properties" : {},
# "geometry": {
# "type": "Polygon",
# "coordinates": [[
# [-10.0, -10.0],
# [10.0, -10.0],
# [10.0, 10.0],
# [-10.0, -10.0]]]
# }
# },
# {
# "type": "Feature",
# "properties" : { "id" : 1, "foo" : false, "bar" : "world" },
# "geometry": {
# "type": "MultiPolygon",
# "coordinates": [
# [[[180.0, 40.0], [180.0, 50.0], [170.0, 50.0],
# [170.0, 40.0], [180.0, 40.0]]],
# [[[-170.0, 40.0], [-170.0, 50.0], [-180.0, 50.0],
# [-180.0, 40.0], [-170.0, 40.0]]]]
# }
# }
# ]'
# sf <- geojson_sf(js)
# sf_geojson(sf)
# geojson_sf( sf_geojson(sf) )



# js <- '{"type":"MultiPolygon","coordinates":[[[[0,0],[0,1],[1,1],[1,0],[0,0]]]]}'
# sf_geojson(geojson_sf(js))

# js <- '{
# "type":"MultiLineString",
# "coordinates":[
# [
# [0,0],[0,1],[1,1],[1,0],[0,0]
# ],
# [
# [2,2],[2,3],[3,3],[3,2],[2,2]
# ]
# ]
# }'
#
# sf_geojson(geojson_sf(js))


# js <- '{
# "type":"MultiPolygon",
# "coordinates":[
# [
# [
# [0,0],[0,1],[1,1],[1,0],[0,0]
# ],
# [
# [0.5,0.5],[0.5,0.75],[0.75,0.75],[0.75,0.5],[0.5,0.5]
# ]
# ],
# [
# [
# [2,2],[2,3],[3,3],[3,2],[2,2]
# ]
# ]
# ]
# }'
#
# sf_geojson(geojson_sf(js), TRUE)


15 changes: 9 additions & 6 deletions inst/include/geojson_wkt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
#define GEOJSON_WKT_H

#include "rapidjson/document.h"
using namespace rapidjson;

void coordSeparateWKT(std::ostringstream& os, int i, int n);
void coord_separator(std::ostringstream& os, int i, int n);

void lineSeparateWKT(std::ostringstream& os, int i, int n);
void line_separator_wkt(std::ostringstream& os, int i, int n);

void polygonSeparateWKT(std::ostringstream& os, int i, int n);
void object_separator(std::ostringstream& os);

void addLonLatToWKTStream(std::ostringstream& os, float lon, float lat );
void polygon_separate_wkt(std::ostringstream& os, int i, int n);

void beginWKT(std::ostringstream& os, std::string& geom_type);
void add_lonlat_to_wkt_stream(std::ostringstream& os, float lon, float lat );

void endWKT(std::ostringstream& os, std::string& geom_type);
void begin_wkt(std::ostringstream& os, std::string& geom_type);

void end_wkt(std::ostringstream& os, std::string& geom_type);

void point_to_wkt(std::ostringstream& os, const Value& coord_array);

Expand Down
30 changes: 30 additions & 0 deletions inst/include/sf_geojson.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef SFGEOJSON_H
#define SFGEOJSON_H

#include <Rcpp.h>
using namespace Rcpp;


//void add_geometrycollection_to_stream(std::ostringstream& os, Rcpp::List& gc);

void begin_geojson_geometry(Rcpp::String& geojson, std::string& geom_type);

void begin_geojson_geometry(Rcpp::String& geojson, Rcpp::List& sfc, std::string& geom_type);

void end_geojson_geometry(Rcpp::String& geojson, std::string& geom_type);

void add_lonlat_to_stream(Rcpp::String& geojson, Rcpp::NumericVector& points);

void fetch_coordinates(Rcpp::String& geojson, Rcpp::List& sfc, int& object_counter);

void coord_separator(Rcpp::String& geojson, int i, int n);

void line_separator_geojson(Rcpp::String& geojson, int i, int n);

void polygon_separator_geojson(Rcpp::String& geojson, int i, int n);

void write_geometry(Rcpp::List& sfg, Rcpp::String& geojson);

void make_gc_type(Rcpp::String& geojson, Rcpp::List& sfg, std::string& geom_type, Rcpp::CharacterVector& cls);

#endif
16 changes: 16 additions & 0 deletions man/sf_geojson.Rd

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

13 changes: 13 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,26 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// rcpp_sf_to_geojson
Rcpp::StringVector rcpp_sf_to_geojson(Rcpp::List sf, bool atomise);
RcppExport SEXP _geojsonsf_rcpp_sf_to_geojson(SEXP sfSEXP, SEXP atomiseSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::List >::type sf(sfSEXP);
Rcpp::traits::input_parameter< bool >::type atomise(atomiseSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_sf_to_geojson(sf, atomise));
return rcpp_result_gen;
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_read_sfc_file", (DL_FUNC) &_geojsonsf_rcpp_read_sfc_file, 1},
{"_geojsonsf_rcpp_read_sf_file", (DL_FUNC) &_geojsonsf_rcpp_read_sf_file, 1},
{"_geojsonsf_rcpp_sf_to_geojson", (DL_FUNC) &_geojsonsf_rcpp_sf_to_geojson, 2},
{NULL, NULL, 0}
};

Expand Down
12 changes: 7 additions & 5 deletions src/geojson_to_wkt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ void parse_geometry_object_wkt(Rcpp::List& sfc,
const Value& coord_array = geometry["coordinates"];
geometry_types.insert(geom_type);

std::ostringstream os;
Rcpp::StringVector wkt;
beginWKT(os, geom_type);
std::ostringstream os;
Rcpp::StringVector wkt;
begin_wkt(os, geom_type);


if (geom_type == "Point") {
point_to_wkt(os, coord_array);
Expand All @@ -52,7 +53,7 @@ void parse_geometry_object_wkt(Rcpp::List& sfc,
Rcpp::stop("unknown sfg type");
}

endWKT(os, geom_type);
end_wkt(os, geom_type);

wkt = os.str();
transform(geom_type.begin(), geom_type.end(), geom_type.begin(), ::toupper);
Expand All @@ -65,6 +66,7 @@ void parse_geometry_object_wkt(Rcpp::List& sfc,
Rcpp::List parse_geometry_collection_object_wkt(const Value& val,
std::set< std::string >& geometry_types,
int& wkt_objects) {

std::string geom_type;

validate_geometries(val, wkt_objects);
Expand All @@ -88,7 +90,7 @@ Rcpp::List parse_geometry_collection_object_wkt(const Value& val,
for (i = 0; i < n; i++) {
std::string g = geom_collection[i];
os << g;
coordSeparateWKT(os, i, n);
coord_separator(os, i, n);
}
os << ")";

Expand Down
Loading