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

Issue68 #72

Merged
merged 15 commits into from
Sep 19, 2019
Merged
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ LinkingTo:
BH,
jsonify (>= 0.2.1),
rapidjsonr (>= 1.1),
Rcpp
Rcpp,
sfheaders
Imports:
curl,
Rcpp
Expand Down
19 changes: 11 additions & 8 deletions inst/include/geojsonsf/geojson/api/df_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
#include "jsonify/to_json/writers/simple.hpp"

#include "geojsonsf/geojsonsf.h"
#include "geojsonsf/utils/where/where.hpp"
#include "geojsonsf/geojson/write_geometry.hpp"

#include "sfheaders/sfg/sfg_dimension.hpp"
#include "sfheaders/utils/vectors/vectors.hpp"

namespace geojsonsf {
namespace api {

inline Rcpp::StringVector df_to_geojson(
Rcpp::DataFrame& df,
Rcpp::StringVector& geometry_columns,
int& digits,
bool& factors_as_string ) {
bool& factors_as_string
) {

rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );
Expand All @@ -26,7 +29,7 @@ namespace api {
Rcpp::StringVector column_names = df.names();

// the sfc_POINT
int n_geometry_columns = geometry_columns.size();
R_xlen_t n_geometry_columns = geometry_columns.size();
Rcpp::List geometry_vectors( n_geometry_columns );

int n_properties = n_cols - n_geometry_columns;
Expand All @@ -37,15 +40,15 @@ namespace api {
geometry_vectors[i] = df[ this_geometry ];
}

std::string dim = geojsonsf::utils::make_dimension( n_geometry_columns );
std::string dim = sfheaders::sfg::sfg_dimension( n_geometry_columns );
Rcpp::CharacterVector cls = Rcpp::CharacterVector::create( dim , "POINT", "sfg");

int property_counter = 0;

for ( i = 0; i < df.length(); i++ ) {

Rcpp::String this_column = column_names[i];
int idx = geojsonsf::utils::where::where_is( this_column, geometry_columns );
int idx = sfheaders::utils::where_is( this_column, geometry_columns );

if ( idx == -1 ) { // i.e. it's not in the vector
property_names[property_counter] = column_names[i];
Expand Down Expand Up @@ -111,7 +114,7 @@ namespace api {
Rcpp::StringVector geojson( n_rows );


int n_geometry_columns = geometry_columns.size();
R_xlen_t n_geometry_columns = geometry_columns.size();
Rcpp::List geometry_vectors( n_geometry_columns );

int n_properties = n_cols - n_geometry_columns;
Expand All @@ -122,14 +125,14 @@ namespace api {
geometry_vectors[i] = df[ this_geometry ];
}

std::string dim = geojsonsf::utils::make_dimension( n_geometry_columns );
std::string dim = sfheaders::sfg::sfg_dimension( n_geometry_columns );
Rcpp::CharacterVector cls = Rcpp::CharacterVector::create( dim , "POINT", "sfg");

int property_counter = 0;
for ( i = 0; i < df.length(); i++) {

Rcpp::String this_column = column_names[i];
int idx = geojsonsf::utils::where::where_is( this_column, geometry_columns );
int idx = sfheaders::utils::where_is( this_column, geometry_columns );

if ( idx == -1 ) { // i.e. it's not in the vector

Expand Down
6 changes: 0 additions & 6 deletions inst/include/geojsonsf/geojson/api/sf_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,7 @@ namespace api {
const char *h = property_names[ j ];

SEXP this_vec = sf[ h ];

//Rcpp::IntegerVector temp = Rcpp::as< Rcpp::IntegerVector >( this_vec );
// Rcpp::Rcout << "iv: " << temp << std::endl;

writer.String( h );
// int xyz;
// Rcpp::Rcout << "factors_as_string " << factors_as_string << std::endl;
jsonify::writers::simple::write_value( writer, this_vec, i, -1, false, factors_as_string );
}
writer.EndObject();
Expand Down
60 changes: 44 additions & 16 deletions inst/include/geojsonsf/geojson/geojson_properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ namespace geojson_properties {
lst[key] = sv;
}

inline void get_property_types(const Value& v, std::unordered_map< std::string, std::string>& property_types) {
inline void get_property_types(
const Value& v,
std::unordered_map< std::string, std::string>& property_types
) {

// TODO: move to a header??
static const char* ARRAY_TYPES[] =
Expand Down Expand Up @@ -59,7 +62,10 @@ namespace geojson_properties {
}


inline void sort_property_names( Rcpp::List& properties, std::unordered_set< std::string >& property_keys) {
inline void sort_property_names(
Rcpp::List& properties,
std::unordered_set< std::string >& property_keys
) {

properties.names() = property_keys;
std::vector< std::string > n = properties.names();
Expand Down Expand Up @@ -88,19 +94,34 @@ namespace geojson_properties {
// }
}

inline void update_string_vector(Rcpp::List& sf, std::string& key, const std::string& value, const int& row_index) {
inline void update_string_vector(
Rcpp::List& sf,
std::string& key,
const std::string& value,
const R_xlen_t& row_index
) {
Rcpp::StringVector sv = sf[key];
sv[row_index] = value;
sf[key] = sv;
}

inline void update_logical_vector(Rcpp::List& sf, std::string& key, const bool& value, const int& row_index) {
inline void update_logical_vector(
Rcpp::List& sf,
std::string& key,
const bool& value,
const R_xlen_t& row_index
) {
Rcpp::LogicalVector lv = sf[key];
lv[row_index] = value;
sf[key] = lv;
}

inline void update_numeric_vector(Rcpp::List& sf, std::string& key, double& value, const int& row_index) {
inline void update_numeric_vector(
Rcpp::List& sf,
std::string& key,
double& value,
const R_xlen_t& row_index
) {
Rcpp::NumericVector nv = sf[key];
nv[row_index] = value;
sf[key] = nv;
Expand All @@ -121,8 +142,11 @@ namespace geojson_properties {
return lv;
}

inline void setup_property_vectors(std::unordered_map< std::string, std::string>& property_types,
Rcpp::List& properties, int& sfg_objects) {
inline void setup_property_vectors(
std::unordered_map< std::string, std::string>& property_types,
Rcpp::List& properties,
R_xlen_t& sfg_objects
) {

for (auto keys_it = property_types.begin(); keys_it != property_types.end(); keys_it++ ) {

Expand All @@ -139,11 +163,13 @@ namespace geojson_properties {
}
}

inline void nested_json_to_string(rapidjson::Value& v,
std::string& type,
Rcpp::List& properties,
int& row_index,
std::string& key) {
inline void nested_json_to_string(
rapidjson::Value& v,
std::string& type,
Rcpp::List& properties,
R_xlen_t& row_index,
std::string& key
) {

StringBuffer sb;
Writer<StringBuffer> writer(sb);
Expand All @@ -159,10 +185,12 @@ namespace geojson_properties {
}
}

inline void fill_property_vectors(Document& doc_properties,
std::unordered_map< std::string, std::string>& property_types,
Rcpp::List& properties,
int& row_index) {
inline void fill_property_vectors(
Document& doc_properties,
std::unordered_map< std::string, std::string>& property_types,
Rcpp::List& properties,
R_xlen_t& row_index
) {

// TODO(move to header):
static const char* ARRAY_TYPES[] =
Expand Down
59 changes: 37 additions & 22 deletions inst/include/geojsonsf/geojson/geojson_to_sf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,27 @@
#include "geojsonsf/geojson/geojson_properties.hpp"
#include "geojsonsf/geojson/parse.hpp"

#include "sfheaders/sfc/bbox.hpp"
#include "sfheaders/sfc/zm_range.hpp"

using namespace rapidjson;

namespace geojsonsf {
namespace sf {

inline Rcpp::List geojson_to_sf(const char* geojson,
Rcpp::NumericVector& bbox,
std::unordered_set< std::string >& geometry_types,
int& sfg_objects,
std::unordered_set< std::string >& property_keys,
Document& doc_properties,
std::unordered_map< std::string, std::string>& property_types,
bool& expand_geometries,
int& nempty) {
inline Rcpp::List geojson_to_sf(
const char* geojson,
Rcpp::NumericVector& bbox,
Rcpp::NumericVector& z_range,
Rcpp::NumericVector& m_range,
std::unordered_set< std::string >& geometry_types,
R_xlen_t& sfg_objects,
std::unordered_set< std::string >& property_keys,
Document& doc_properties,
std::unordered_map< std::string, std::string>& property_types,
bool& expand_geometries,
R_xlen_t& nempty
) {

Document d;
geojsonsf::validate::safe_parse(d, geojson);
Expand All @@ -42,7 +49,7 @@ namespace sf {
if (d.IsObject()) {
Rcpp::List sfg(1);
geojsonsf::geojson::parse::parse_geojson_object(
d, sfg, properties, bbox, geometry_types, sfg_objects, property_keys,
d, sfg, properties, bbox, z_range, m_range, geometry_types, sfg_objects, property_keys,
doc_properties, property_types, expand_geometries, nempty
);
sfc[0] = sfg;
Expand All @@ -53,7 +60,7 @@ namespace sf {

for (doc_ele = 0; doc_ele < d.Size(); doc_ele++) {
geojsonsf::geojson::parse::parse_geojson_array(
d, sfgs, properties, doc_ele, bbox, geometry_types, sfg_objects,
d, sfgs, properties, doc_ele, bbox, z_range, m_range, geometry_types, sfg_objects,
property_keys, doc_properties, property_types, expand_geometries, nempty
);
}
Expand All @@ -65,12 +72,16 @@ namespace sf {
inline Rcpp::List create_sfc(Rcpp::StringVector geojson, bool& expand_geometries) {
// iterate over the geojson
int n = geojson.size();
int sfg_objects = 0; // keep track of number of objects
int nempty = 0;
R_xlen_t sfg_objects = 0; // keep track of number of objects
R_xlen_t nempty = 0;
//int row_index;

// Attributes to keep track of along the way
Rcpp::NumericVector bbox = geojsonsf::sfc::utils::start_bbox();
//Rcpp::NumericVector bbox = geojsonsf::sfc::utils::start_bbox();
Rcpp::NumericVector bbox = sfheaders::bbox::start_bbox();
Rcpp::NumericVector z_range = sfheaders::zm::start_z_range();
Rcpp::NumericVector m_range = sfheaders::zm::start_m_range();

std::unordered_set< std::string > geometry_types;
std::unordered_set< std::string > property_keys; // storing all the 'key' values from 'properties'
std::unordered_map< std::string, std::string> property_types;
Expand All @@ -81,23 +92,27 @@ namespace sf {

for (int geo_ele = 0; geo_ele < n; geo_ele++ ){
sfc[geo_ele] = geojson_to_sf(
geojson[geo_ele], bbox, geometry_types, sfg_objects, property_keys,
geojson[geo_ele], bbox, z_range, m_range, geometry_types, sfg_objects, property_keys,
doc_properties, property_types, expand_geometries, nempty
);
}

return geojsonsf::sfc::construct_sfc(sfg_objects, sfc, bbox, geometry_types, nempty);
return geojsonsf::sfc::construct_sfc(sfg_objects, sfc, bbox, z_range, m_range, geometry_types, nempty);
}

inline Rcpp::List generic_geojson_to_sf(Rcpp::StringVector geojson, bool& expand_geometries) {
// iterate over the geojson
int n = geojson.size();
int sfg_objects = 0; // keep track of number of objects
int row_index = 0;
int nempty = 0;
R_xlen_t sfg_objects = 0; // keep track of number of objects
R_xlen_t row_index = 0;
R_xlen_t nempty = 0;

// Attributes to keep track of along the way
Rcpp::NumericVector bbox = geojsonsf::sfc::utils::start_bbox();
//Rcpp::NumericVector bbox = geojsonsf::sfc::utils::start_bbox();
Rcpp::NumericVector bbox = sfheaders::bbox::start_bbox();
Rcpp::NumericVector z_range = sfheaders::zm::start_z_range();
Rcpp::NumericVector m_range = sfheaders::zm::start_m_range();

std::unordered_set< std::string > geometry_types;
std::unordered_set< std::string > property_keys; // storing all the 'key' values from 'properties'
std::unordered_map< std::string, std::string > property_types;
Expand All @@ -108,12 +123,12 @@ namespace sf {

for ( int geo_ele = 0; geo_ele < n; geo_ele++ ){
sfc[geo_ele] = geojson_to_sf(
geojson[geo_ele], bbox, geometry_types, sfg_objects, property_keys,
geojson[geo_ele], bbox, z_range, m_range, geometry_types, sfg_objects, property_keys,
doc_properties, property_types, expand_geometries, nempty
);
}

Rcpp::List res = geojsonsf::sfc::construct_sfc( sfg_objects, sfc, bbox, geometry_types, nempty );
Rcpp::List res = geojsonsf::sfc::construct_sfc( sfg_objects, sfc, bbox, z_range, m_range, geometry_types, nempty );
return geojsonsf::sf::construct_sf( res, property_keys, property_types, doc_properties, sfg_objects, row_index );
}

Expand Down
16 changes: 8 additions & 8 deletions inst/include/geojsonsf/geojson/geojson_validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ namespace validate {
}
}

inline void validate_array(const Value& v, int& sfg_objects) {
inline void validate_array(const Value& v, R_xlen_t& sfg_objects) {
if ( v.IsArray() == false) {
geojson_object_error("array", sfg_objects);
}
}


inline void validate_type(const Value& v, int& sfg_objects) {
inline void validate_type(const Value& v, R_xlen_t& sfg_objects) {
if (v.HasMember("type") == false ) {
geojson_object_error("type", sfg_objects);
}
Expand All @@ -48,37 +48,37 @@ namespace validate {
}
}

inline void validate_features(const Value& v, int& sfg_objects) {
inline void validate_features(const Value& v, R_xlen_t& sfg_objects) {
if (v.HasMember("features") == false) {
geojson_object_error("features", sfg_objects);
}
}

inline void validate_feature(const Value& v, int& sfg_objects) {
inline void validate_feature(const Value& v, R_xlen_t& sfg_objects) {
if (v.HasMember("feature") == false) {
geojson_object_error("feature", sfg_objects);
}
}

inline void validate_properties(const Value& v, int& sfg_objects) {
inline void validate_properties(const Value& v, R_xlen_t& sfg_objects) {
if (v.HasMember("properties") == false) {
geojson_object_error("properties", sfg_objects);
}
}

inline void validate_geometry(const Value& v, int& sfg_objects) {
inline void validate_geometry(const Value& v, R_xlen_t& sfg_objects) {
if (v.HasMember("geometry") == false) {
geojson_object_error("geometry", sfg_objects);
}
}

inline void validate_geometries(const Value& v, int& sfg_objects) {
inline void validate_geometries(const Value& v, R_xlen_t& sfg_objects) {
if (v.HasMember("geometries") == false) {
geojson_object_error("geometries", sfg_objects);
}
}

inline void validate_coordinates(const Value& v, int& sfg_objects) {
inline void validate_coordinates(const Value& v, R_xlen_t& sfg_objects) {
if (v.HasMember("coordinates") == false) {
geojson_object_error("coordinates", sfg_objects);
}
Expand Down
Loading