geojson文件示例,
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102.0, 0.5]
},
"properties": {
"name": "Example Point"
}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0],
[103.0, 1.0],
[104.0, 0.0],
[105.0, 1.0]
]
},
"properties": {
"name": "Example Line"
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0]
]
]
},
"properties": {
"name": "Example Polygon"
}
}
]
}
geojson示例2,
{
"features": [
{
"geometry": {
"coordinates": [
[
116.27394289090265,
40.04563683773862,
38.911601834233437
],
[
116.27394460815701,
40.04562333499269,
38.908991369866669
]
],
"type": "LineString"
},
"properties": {
"line_color": "3",
"line_conf_type": "196.507625",
"line_type": "7",
"source": "optimized_line_feature"
},
"type": "Feature"
},
{
"geometry": {
"coordinates": [
[
116.27372626712896,
40.04566908578874,
38.163651788610785
],
[
116.27372924922214,
40.04565573741756,
38.165627857154753
]
],
"type": "LineString"
},
"properties": {
"line_color": "3",
"line_conf_type": "196.507625",
"line_type": "7",
"source": "optimized_line_feature"
},
"type": "Feature"
},
{
"geometry": {
"coordinates": [
[
116.2736425888071,
40.04585037786518,
37.983635180289525
],
[
116.2736420649746,
40.04583632046637,
37.972707995880953
]
],
"type": "LineString"
},
"properties": {
"line_color": "3",
"line_conf_type": "196.507625",
"line_type": "7",
"source": "optimized_line_feature"
},
"type": "Feature"
}
],
"name": "SingleTrajectoryRes",
"type": "FeatureCollection"
}
生成geojson文件的C++代码示例,
void GlobalMappingImpl::generate_geojson_gnss() const {
Json::Value root;
root.clear();
root["type"] = "FeatureCollection";
root["name"] = "SingleTrajectoryGlobalRes";
auto& root_features = root["features"];
auto _it_meas = _map_gnss._meas_map.begin();
while (_map_gnss._meas_map.size() >= 1 && _it_meas != _map_gnss._meas_map.end()) {
Json::Value json;
json["type"] = "Feature";
auto& json_properties = json["properties"];
json_properties["name"] = "GNSS";
json_properties["head_veh"].append(_it_meas->second->head_veh());
json_properties["fix_type"].append(_it_meas->second->fix_type());
auto& json_geometry = json["geometry"];
json_geometry["type"] = "Point";
json_geometry["coordinates"].append(_it_meas->second->lon());
json_geometry["coordinates"].append(_it_meas->second->lat());
root_features.append(json);
_it_meas++;
}
static Json::FastWriter fast_write;
auto* file = new std::ofstream("./debug_optimized_gnss.geojson", std::ios::trunc);
if (file->is_open()) {
*file << fast_write.write(root);
file->flush();
file->close();
}
delete file;
}
C++代码示例2,
//output.geo.json
for (auto partition : _partition_set.partitions) {
int id = partition.partition_id;
Json::Value root;
root["type"] = "FeatureCollection";
root["name"] = "SingleTrajectoryGlobalRes";
auto &features = root["features"];
for (auto lanemarking : partition.lanemarking_segment_set) {
Json::Value feature;
feature["type"] = "Feature";
auto &properties = feature["properties"];
properties["marking_id"] = lanemarking.marking_id;
properties["if_edge"] = lanemarking.if_edge;
auto &geometry = feature["geometry"];
geometry["type"] = "LineString";
auto &coordinates = geometry["coordinates"];
for (auto point : lanemarking.marking_points) {
double x = point.geometry[1];
double y = point.geometry[0];
double z = point.geometry[2];
Json::Value coord;
coord.append(x);
coord.append(y);
coord.append(z);
coordinates.append(coord);
}
features.append(feature);
}
std::string geo_save_dir_path = "log";
if (!boost::filesystem::exists(geo_save_dir_path)) {
boost::filesystem::create_directories(geo_save_dir_path);
}
std::string geojson_save_path = geo_save_dir_path + "/partitionid" + std::to_string(id) + ".geo.json";
Json::FastWriter fast_write;
std::ofstream file(geojson_save_path, std::ios::trunc);
if (file.is_open()) {
file << fast_write.write(root);
file.flush();
file.close();
}
}
python3代码示例1,
def save_lanelines_to_geojson(save_file_path: str, lanelines: list) -> None:
outjson = {}
outjson["type"] = "FeatureCollection"
outjson["features"] = []
for i,laneline in enumerate(lanelines):
newlaneline = list(laneline)
newlaneline = [[float(p[0]),float(p[1]),float(p[2])] for p in newlaneline]
feature = {}
feature["type"] = "Feature"
feature["properties"] = {}
feature["properties"]["idx"] = i
feature["geometry"] = {}
feature["geometry"]["type"] = "LineString"
feature["geometry"]["coordinates"] = newlaneline
outjson["features"].append(feature)
with open(save_file_path, "w", encoding = "utf-8") as fw:
json.dump(outjson, fw)
return