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

Support id for GeoJSONFeature #994

Merged
merged 3 commits into from
Nov 22, 2023
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
11 changes: 11 additions & 0 deletions include/geos/io/GeoJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ class GEOS_DLL GeoJSONFeature {
GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
std::map<std::string, GeoJSONValue>&& p);

GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
const std::map<std::string, GeoJSONValue>& p,
const std::string& id);

GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
std::map<std::string, GeoJSONValue>&& p, std::string id);

GeoJSONFeature(GeoJSONFeature const& other);

GeoJSONFeature(GeoJSONFeature&& other);
Expand All @@ -110,12 +117,16 @@ class GEOS_DLL GeoJSONFeature {

const std::map<std::string, GeoJSONValue>& getProperties() const;

const std::string& getId() const;

private:

std::unique_ptr<geom::Geometry> geometry;

std::map<std::string, GeoJSONValue> properties;

std::string id;

};

class GEOS_DLL GeoJSONFeatureCollection {
Expand Down
20 changes: 16 additions & 4 deletions src/io/GeoJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,26 @@ bool GeoJSONValue::isArray() const
// GeoJSONFeature

GeoJSONFeature::GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
const std::map<std::string, GeoJSONValue>& p) : geometry(std::move(g)), properties(p) {}
const std::map<std::string, GeoJSONValue>& p) : geometry(std::move(g)), properties(p), id("") {}

GeoJSONFeature::GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
std::map<std::string, GeoJSONValue>&& p) : geometry(std::move(g)), properties(std::move(p)) {}
std::map<std::string, GeoJSONValue>&& p) : geometry(std::move(g)), properties(std::move(p)), id("") {}

GeoJSONFeature::GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
const std::map<std::string, GeoJSONValue>& p,
const std::string& i)
: geometry(std::move(g)), properties(p), id(i) {}

GeoJSONFeature::GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
std::map<std::string, GeoJSONValue>&& p,
std::string i)
: geometry(std::move(g)), properties(std::move(p)), id(std::move(i)) {}

GeoJSONFeature::GeoJSONFeature(GeoJSONFeature const& other) : geometry(other.geometry->clone()),
properties(other.properties) {}
properties(other.properties), id(other.id) {}

GeoJSONFeature::GeoJSONFeature(GeoJSONFeature&& other) : geometry(std::move(other.geometry)),
properties(std::move(other.properties)) {}
properties(std::move(other.properties)), id(std::move(other.id)) {}

GeoJSONFeature& GeoJSONFeature::operator=(const GeoJSONFeature& other)
{
Expand Down Expand Up @@ -262,6 +272,8 @@ const std::map<std::string, GeoJSONValue>& GeoJSONFeature::getProperties() const
return properties;
}

const std::string& GeoJSONFeature::getId() const { return id; }

// GeoJSONFeatureCollection

GeoJSONFeatureCollection::GeoJSONFeatureCollection(const std::vector<GeoJSONFeature>& f) : features(f) {}
Expand Down
9 changes: 8 additions & 1 deletion src/io/GeoJSONReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ GeoJSONFeature GeoJSONReader::readFeature(const geos_nlohmann::json& j) const
{
const auto& geometryJson = j.at("geometry");
const auto& properties = j.at("properties");
return GeoJSONFeature{readGeometry(geometryJson), readProperties(properties)};

std::string id = "";
if (j.contains("id") && !j.at("id").is_null()) {
if (j.at("id").is_string()) id = j.at("id").get<std::string>();
if (j.at("id").is_number()) id = j.at("id").dump();
}

return GeoJSONFeature{readGeometry(geometryJson), readProperties(properties), id};
}

std::map<std::string, GeoJSONValue> GeoJSONReader::readProperties(
Expand Down
4 changes: 4 additions & 0 deletions src/io/GeoJSONWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ std::string GeoJSONWriter::write(const GeoJSONFeatureCollection& features)
void GeoJSONWriter::encodeFeature(const GeoJSONFeature& feature, geos_nlohmann::ordered_json& j)
{
j["type"] = "Feature";

if (feature.getId().size() > 0) j["id"] = feature.getId();

json geometryJson;
encodeGeometry(feature.getGeometry(), geometryJson);
j["geometry"] = geometryJson;

json propertiesJson = json::object();
for (auto const& property : feature.getProperties()) {
std::string key = property.first;
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/io/GeoJSONReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,14 @@ template<>
void object::test<19>
()
{
std::string geojson { "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-117.0,33.0]}, \"properties\": {\"id\": 1, \"name\": \"one\", \"items\": [1,2,3,4], \"nested\": {\"id\":2, \"name\":\"two\"}}}" };
std::string geojson { "{\"type\":\"Feature\",\"id\":\"id123\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-117.0,33.0]}, \"properties\": {\"id\": 1, \"name\": \"one\", \"items\": [1,2,3,4], \"nested\": {\"id\":2, \"name\":\"two\"}}}" };
geos::io::GeoJSONFeatureCollection features(geojsonreader.readFeatures(geojson));
ensure_equals(features.getFeatures().size(), static_cast<size_t>(1));
ensure_equals(static_cast<size_t>(features.getFeatures()[0].getGeometry()->getCoordinateDimension()), 2u);
ensure_equals(features.getFeatures()[0].getGeometry()->toText(), "POINT (-117 33)");
ensure_equals(features.getFeatures()[0].getProperties().at("id").getNumber(), 1.0);
ensure_equals(features.getFeatures()[0].getProperties().at("name").getString(), "one");
ensure_equals(features.getFeatures()[0].getId(), "id123");
std::vector<geos::io::GeoJSONValue> values = features.getFeatures()[0].getProperties().at("items").getArray();
ensure_equals(values.size(), static_cast<size_t>(4));
ensure_equals(values[0].getNumber(), 1.0);
Expand Down Expand Up @@ -476,5 +477,33 @@ void object::test<30>
ensure(errorMessage.find("ParseException: Error parsing JSON") != std::string::npos);
}

// Read a GeoJSON FeatureCollection with multiple Features with id
template<>
template<>
void object::test<31>
()
{
std::string geojson { "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\", \"id\":\"123\", \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}},"
"{\"type\":\"Feature\", \"id\": 123, \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}},"
"{\"type\":\"Feature\", \"id\": 123.0, \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}},"
"{\"type\":\"Feature\", \"id\": 123.000, \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}},"
"{\"type\":\"Feature\", \"id\": 123.9, \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}},"
"{\"type\":\"Feature\", \"id\": null, \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}},"
"{\"type\":\"Feature\", \"id\": {}, \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}},"
"{\"type\":\"Feature\", \"id\": [\"123\"], \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}},"
"{\"type\":\"Feature\", \"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}, \"properties\":{}}]}" };
geos::io::GeoJSONFeatureCollection features(geojsonreader.readFeatures(geojson));
ensure_equals(features.getFeatures().size(), static_cast<size_t>(9));
ensure_equals(features.getFeatures()[0].getId(), "123");
ensure_equals(features.getFeatures()[1].getId(), "123");
ensure_equals(features.getFeatures()[2].getId(), "123.0");
ensure_equals(features.getFeatures()[3].getId(), "123.0");
ensure_equals(features.getFeatures()[4].getId(), "123.9");
ensure_equals(features.getFeatures()[5].getId(), "");
ensure_equals(features.getFeatures()[6].getId(), "");
ensure_equals(features.getFeatures()[7].getId(), "");
ensure_equals(features.getFeatures()[8].getId(), "");
}

}

6 changes: 3 additions & 3 deletions tests/unit/io/GeoJSONWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ void object::test<14>
geos::io::GeoJSONFeatureCollection features {{
geos::io::GeoJSONFeature { wktreader.read("POINT(-117 33)"), std::map<std::string, geos::io::GeoJSONValue> {
{"id", geos::io::GeoJSONValue(1.0) },
{"name", geos::io::GeoJSONValue(std::string{"One"}) },
}},
{"name", geos::io::GeoJSONValue(std::string{"One"})}},
"id123"},
geos::io::GeoJSONFeature { wktreader.read("POINT(-127 53)"), std::map<std::string, geos::io::GeoJSONValue> {
{"id", geos::io::GeoJSONValue(2.0) },
{"name", geos::io::GeoJSONValue(std::string{"Two"}) },
}}
}};
std::string result = geojsonwriter.write(features);
ensure_equals(result, "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-117.0,33.0]},\"properties\":{\"id\":1.0,\"name\":\"One\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-127.0,53.0]},\"properties\":{\"id\":2.0,\"name\":\"Two\"}}]}");
ensure_equals(result, "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"id\":\"id123\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-117.0,33.0]},\"properties\":{\"id\":1.0,\"name\":\"One\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-127.0,53.0]},\"properties\":{\"id\":2.0,\"name\":\"Two\"}}]}");
}

// Write an empty point
Expand Down