generated from cubao/cmake_example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test json * not ready * rapidjson * rapidjson io * not ready * update * rapidjson io * has from to rapidjson * more bindings * lint * why offset not inited? * fix * add rapidjson * update * add geometry * good * indexer * add indexer * not ready * loading geojson * fix * fix * fix --------- Co-authored-by: TANG ZHIXIONG <[email protected]>
- Loading branch information
1 parent
0f27c44
commit f42ff04
Showing
11 changed files
with
808 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
from typing import Union | ||
|
||
from loguru import logger | ||
|
||
from nano_fmm import Indexer, rapidjson | ||
|
||
|
||
def remap_network_with_string_id( | ||
network: Union[str, rapidjson], | ||
*, | ||
export: str = None, | ||
): | ||
""" | ||
network.json normally has: | ||
{ | ||
"type": "Feature", | ||
"geometry": {...}, | ||
"properties": { | ||
"id": 244, | ||
"nexts": [326, 452], | ||
"prevs": [5241, 563], | ||
... | ||
}, | ||
} | ||
if you have: | ||
{ | ||
"id": "road1", | ||
"nexts": ["road2", "road3"], | ||
"prevs": ["road4", "road5"], | ||
} | ||
""" | ||
if isinstance(network, str): | ||
path = network | ||
network = rapidjson() | ||
network.load(path) | ||
indexer = Indexer() | ||
features = network["features"] | ||
for i in range(len(features)): | ||
f = features[i] | ||
props = f["properties"] | ||
if "id" not in props or "nexts" not in props and "prevs" not in props: | ||
continue | ||
props["id"] = indexer.id(props["id"]()) | ||
props["nexts"] = [indexer.id(n) for n in props["nexts"]()] | ||
props["prevs"] = [indexer.id(n) for n in props["prevs"]()] | ||
if "folds" in props: | ||
props["folds"][0] = [indexer.id(i) for i in props["folds"][0]()] | ||
props["type"] = "road" | ||
if export: | ||
network["index"] = indexer.to_rapidjson() | ||
export = os.path.abspath(export) | ||
os.makedirs(os.path.dirname(export), exist_ok=True) | ||
network.dump(export, indent=True) | ||
logger.info(f"wrote to {export}") | ||
return network, indexer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
#pragma once | ||
|
||
#include "nano_fmm/types.hpp" | ||
|
||
namespace nano_fmm | ||
{ | ||
struct Config | ||
{ | ||
double ubodt_thresh = 3000.0; | ||
|
||
SETUP_FLUENT_API(Config, double, ubodt_thresh) | ||
Config &from_rapidjson(const RapidjsonValue &json); | ||
RapidjsonValue to_rapidjson(RapidjsonAllocator &allocator) const; | ||
RapidjsonValue to_rapidjson() const | ||
{ | ||
RapidjsonAllocator allocator; | ||
return to_rapidjson(allocator); | ||
} | ||
|
||
private: | ||
double ubodt_thresh_ = 3000.0; | ||
}; | ||
} // namespace nano_fmm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#pragma once | ||
|
||
#include <unordered_map> | ||
#include <string> | ||
#include <iostream> | ||
#include "nano_fmm/types.hpp" | ||
|
||
namespace nano_fmm | ||
{ | ||
struct Indexer | ||
{ | ||
// get str id (with auto setup) | ||
std::string id(int64_t id) | ||
{ | ||
auto itr = int2str_.find(id); | ||
if (itr != int2str_.end()) { | ||
return itr->second; | ||
} | ||
int round = 0; | ||
auto id_str = std::to_string(id); | ||
auto str_id = id_str; | ||
while (str2int_.count(str_id)) { | ||
++round; | ||
str_id = id_str + "/" + std::to_string(round); | ||
} | ||
index(str_id, id); | ||
return str_id; | ||
} | ||
// get int id (with auto setup) | ||
int64_t id(const std::string &id) | ||
{ | ||
auto itr = str2int_.find(id); | ||
if (itr != str2int_.end()) { | ||
return itr->second; | ||
} | ||
try { | ||
// '44324' -> 44324 | ||
// 'w44324' -> 44324 | ||
int64_t ii = | ||
id[0] == 'w' ? std::stoll(id.substr(1)) : std::stoll(id); | ||
if (index(id, ii)) { | ||
return ii; | ||
} | ||
} catch (...) { | ||
} | ||
while (!index(id, id_cursor_)) { | ||
++id_cursor_; | ||
} | ||
return id_cursor_++; | ||
} | ||
// setup str/int id, returns true (setup) or false (skip) | ||
bool index(const std::string &str_id, int64_t int_id) | ||
{ | ||
if (str2int_.count(str_id) || int2str_.count(int_id)) { | ||
return false; | ||
} | ||
str2int_.emplace(str_id, int_id); | ||
int2str_.emplace(int_id, str_id); | ||
return true; | ||
} | ||
std::map<std::string, int64_t> index() const | ||
{ | ||
return {str2int_.begin(), str2int_.end()}; | ||
} | ||
|
||
Indexer &from_rapidjson(const RapidjsonValue &json) | ||
{ | ||
// for (auto &m: json.GetMe) | ||
for (auto &m : json.GetObject()) { | ||
index(std::string(m.name.GetString(), m.name.GetStringLength()), | ||
m.value.GetInt64()); | ||
} | ||
return *this; | ||
} | ||
RapidjsonValue to_rapidjson(RapidjsonAllocator &allocator) const | ||
{ | ||
RapidjsonValue json(rapidjson::kObjectType); | ||
for (auto &pair : str2int_) { | ||
auto &str = pair.first; | ||
json.AddMember(RapidjsonValue(str.data(), str.size(), allocator), | ||
RapidjsonValue(pair.second), allocator); | ||
} | ||
std::sort( | ||
json.MemberBegin(), json.MemberEnd(), [](auto &lhs, auto &rhs) { | ||
return strcmp(lhs.name.GetString(), rhs.name.GetString()) < 0; | ||
}); | ||
return json; | ||
} | ||
RapidjsonValue to_rapidjson() const | ||
{ | ||
RapidjsonAllocator allocator; | ||
return to_rapidjson(allocator); | ||
} | ||
|
||
private: | ||
std::unordered_map<std::string, int64_t> str2int_; | ||
std::unordered_map<int64_t, std::string> int2str_; | ||
int64_t id_cursor_{1000000}; | ||
}; | ||
} // namespace nano_fmm |
Oops, something went wrong.