Creating Single Column GeoJSON using R
In this article, we will explore how to create a single column Excel file containing self-contained GeoJSON objects using R and the openxlsx package. The goal is to have an Excel file where each row represents a separate feature with its own GeoJSON object.
GeoJSON is a JSON-based format for representing geographical data. It has become widely used in various applications, including mapping services, geospatial analysis, and more. When working with GeoJSON data in R, we often need to convert it into an Excel file that can be easily imported and manipulated in Power Apps or other geospatial-related tools.
Prerequisites
Before diving into the solution, make sure you have the following packages installed:
openxlsx
jsonlite
(for reading and writing GeoJSON files)
You can install these packages using the following command:
install.packages("openxlsx")
install.packages("jsonlite")
Step 1: Prepare Your Input Data
First, ensure that your input data is in a suitable format. For this example, we will use an existing GeoJSON object stored as a string variable named bob
.
Reading and Displaying the Existing GeoJSON Object
Here’s how to read and display the existing GeoJSON object using R:
# Install required packages if not already installed
install.packages("jsonlite")
library(jsonlite)
# Define the existing GeoJSON object
bob <- '{"type":"FeatureCollection","properties":{"kind":"state","state":"WA"},"features":[{"type":"Feature","properties":{"kind":"county","name":"Adams","state":"WA"},"geometry":{"type":"MultiPolygon","coordinates":[[[[-118.9503,47.2640],[-117.9590,47.2586],[-117.9699,46.8697],[-118.0466,46.7711],[-118.2109,46.7383],[-119.2132,46.7383],[-119.3720,46.7383],[-119.3665,46.9135],[-118.9832,46.9135],[-118.9777,47.2640]]]]}}]}'
# Display the GeoJSON object
print(bob)
This code reads the existing GeoJSON object from the bob
variable and prints it to the console.
Step 2: Convert GeoJSON Object into a DataFrame
To create an Excel file with separate rows for each feature, we need to convert the GeoJSON object into a suitable data frame format. This can be achieved by using the jsonlite
package to read the GeoJSON object and then converting it into a data frame.
Here’s how you can do this:
# Convert the GeoJSON object into a data frame
library(jsonlite)
test2 <- data.frame(
County = "Adams",
GeoJson = bob,
Color = "RGB(184,210,232)"
)
In this step, we create a new data frame test2
that includes the existing bob
variable as its GeoJson
column.
Step 3: Write the Data Frame to an Excel File
Finally, we can use the openxlsx
package to write the test2
data frame to an Excel file named “test2.xls”. This will result in an Excel file where each row represents a separate feature with its own GeoJSON object.
Here’s how you can do this:
# Write the data frame to an Excel file
library(openxlsx)
openxlsx::write.xlsx(test2, "test2.xls")
This code uses the openxlsx
package to write the test2
data frame to an Excel file named “test2.xls”.
Additional Example: Testing GeoJSON from a File
As a bonus example, we will demonstrate how to read and process a GeoJSON object directly from a file.
Here’s how you can do this:
# Read the GeoJSON file into a variable
bill <- readLines("test.geojson")
# Display the contents of the file
print(bill)
This code reads the contents of the “test.geojson” file and prints it to the console.
Writing the GeoJSON Object from a File
We can now write this bill
variable back into an Excel file using the same openxlsx
package:
# Write the contents of the 'bill' variable to an Excel file
library(openxlsx)
test3 <- data.frame(
County = "Adams",
GeoJson = bill,
Color = "RGB(184,210,232)"
)
openxlsx::write.xlsx(test3, "test3.xls")
In this final example, we create a new data frame test3
that includes the bill
variable as its GeoJson
column. We then write this data frame to an Excel file named “test3.xls”.
Conclusion
Creating single column GeoJSON using R can be achieved by first reading and displaying the existing GeoJSON object, converting it into a suitable data frame format, and finally writing the data frame to an Excel file.
With these steps and examples, you should now have a solid understanding of how to work with GeoJSON objects in R. Whether your input data is stored as a string variable or directly from a file, you can apply similar techniques to convert it into an Excel file containing self-contained GeoJSON objects.
Creating Multiple Files Using the writeLines
Function
To create multiple files using the writeLines
function, you can modify the existing code to write each feature’s GeoJSON object into its own separate file:
# Define a list of features
features <- list(
list(type = "Feature", properties = list(kind = "county", name = "Adams"), geometry = list(type = "MultiPolygon", coordinates = [[[[-118.9503,47.2640],[-117.9590,47.2586],[-117.9699,46.8697],[-118.0466,46.7711],[-118.2109,46.7383],[-119.2132,46.7383],[-119.3720,46.7383],[-119.3665,46.9135],[-118.9832,46.9135],[-118.9777,47.2640]]]])),
list(type = "Feature", properties = list(kind = "state", state = "WA"), geometry = list(type = "MultiPolygon", coordinates = [[[[-118.9503,47.2640],[-117.9590,47.2586],[-117.9699,46.8697],[-118.0466,46.7711],[-118.2109,46.7383],[-119.2132,46.7383],[-119.3720,46.7383],[-119.3665,46.9135],[-118.9832,46.9135],[-118.9777,47.2640]]]]))
)
# Write each feature's GeoJSON object into its own file
for (i in 1:length(features)) {
writeLines(features[[i]], paste0("feature", i, ".geojson"))
}
This code creates a list of features and then uses the writeLines
function to write each feature’s GeoJSON object into its own separate file.
Last modified on 2024-08-22