Skip to content

Commit

Permalink
re-introduce GeoJSON support
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless committed Mar 16, 2024
1 parent 4b18b16 commit 72e2f9a
Show file tree
Hide file tree
Showing 62 changed files with 3,072 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: yyjsonr
Type: Package
Title: Fast JSON Parser and Generator
Version: 0.1.18.9004
Version: 0.1.18.9005
Authors@R: c(
person("Mike", "Cheng", role = c("aut", "cre", 'cph'),
email = "[email protected]"),
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(opts_read_geojson)
export(opts_read_json)
export(opts_write_geojson)
export(opts_write_json)
export(read_geojson_file)
export(read_geojson_str)
export(read_json_conn)
export(read_json_file)
export(read_json_raw)
Expand All @@ -10,6 +14,8 @@ export(read_ndjson_file)
export(read_ndjson_str)
export(validate_json_file)
export(validate_json_str)
export(write_geojson_file)
export(write_geojson_str)
export(write_json_file)
export(write_json_str)
export(write_ndjson_file)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

# yyjsonr 0.1.18.9005 2024-03-16

* Re-introduce GeoJSON support

# yyjsonr 0.1.18.9004 2024-03-15

Expand Down
131 changes: 131 additions & 0 deletions R/geojson.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Options for reading in GeoJSON
#'
#' @param property_promotion What is the most general container type to use when
#' properties differ across a FEATURECOLLECTION? E.g. if the property
#' exists both as a numeric and a string, should all values be promoted
#' to a 'string', or contained as different types in a 'list'.
#' Default: 'string' will behave like 'geojsonsf'
#' @param type 'sf' or 'sfc'
#' @param property_promotion_lgl_as_int when promoting properties into a string,
#' should logical values become strings e.g. "TRUE" or integers
#' e.g. "1". Default: "integer" in order to match `geojsonsf` packages
#'
#' @return named list
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
opts_read_geojson <- function(type = c('sf', 'sfc'),
property_promotion = c('string', 'list'),
property_promotion_lgl_as_int = c('integer', 'string')) {
structure(
list(
type = match.arg(type),
property_promotion = match.arg(property_promotion)
),
class = "opts_read_geojson"
)
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Options for writing from \code{sf} object to \code{GeoJSON}
#'
#' Currently no options available.
#'
#' @return named list of options
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
opts_write_geojson <- function() {
structure(
list(

),
class = "opts_write_geojson"
)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Load GeoJSON as \code{sf} object
#'
#' @param filename filename
#' @param str single character string containing GeoJSON
#' @param opts named list of options. Usually created with \code{opts_read_geojson()}.
#' Default: empty \code{list()} to use the default options.
#' @param ... any extra named options override those in \code{opts}
#'
#' @return \code{sf} object
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
read_geojson_str <- function(str, opts = list(), ...) {
opts <- modify_list(opts, list(...))

.Call(
parse_geojson_str_,
str,
opts, # geojson parse opts
list(yyjson_read_flag = 0L) # general parse opts
)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname read_geojson_str
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
read_geojson_file <- function(filename, opts = list(), ...) {
opts <- modify_list(opts, list(...))

.Call(
parse_geojson_file_,
filename,
opts, # geojson parse opts
list(yyjson_read_flag = 0L) # general parse opts
)
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Write SF to GeoJSON string
#'
#' @param x \code{sf} object. Supports \code{sf} or \code{sfc}
#' @param filename filename
#' @param opts named list of options. Usually created with \code{opts_write_geojson()}.
#' Default: empty \code{list()} to use the default options.
#' @param ... any extra named options override those in \code{opts}
#' @param digits decimal places to keep for floating point numbers. Default: -1.
#' Positive values specify number of decimal places. Using zero will
#' write the numeric value as an integer. Values less than zero mean that
#' the floating point value should be written as-is (the default).
#'
#' @return character string containing json
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
write_geojson_str <- function(x, opts = list(), ..., digits = -1) {
opts <- modify_list(opts, list(...))

.Call(
serialize_sf_to_str_,
x,
opts, # geojson serialize opts
list(yyjson_write_flag = 0L, digits = digits) # general serialize opts
)
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname write_geojson_str
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
write_geojson_file <- function(x, filename, opts = list(), ..., digits = -1) {
opts <- modify_list(opts, list(...))

.Call(
serialize_sf_to_file_,
x,
filename,
opts, # geojson serialize opts
list(yyjson_write_flag = 0L, digits = digits) # general serialize opts
)

invisible()
}
31 changes: 31 additions & 0 deletions man/opts_read_geojson.Rd

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

14 changes: 14 additions & 0 deletions man/opts_write_geojson.Rd

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

27 changes: 27 additions & 0 deletions man/read_geojson_str.Rd

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

32 changes: 32 additions & 0 deletions man/write_geojson_str.Rd

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

Loading

0 comments on commit 72e2f9a

Please sign in to comment.