|
| 1 | +// Fill out your copyright notice in the Description page of Project Settings. |
| 2 | + |
| 3 | + |
| 4 | +#include "GeoJsonStructGeneratorComponent.h" |
| 5 | + |
| 6 | +#include "JsonUtilities.h" |
| 7 | + |
| 8 | +#include "Developer/DesktopPlatform/Public/IDesktopPlatform.h" |
| 9 | +#include "Developer/DesktopPlatform/Public/DesktopPlatformModule.h" |
| 10 | +#include <vector> |
| 11 | +#include <string> |
| 12 | +#include <nlohmann/json.hpp> |
| 13 | +#include <fstream> |
| 14 | + |
| 15 | +using namespace std; |
| 16 | +using json = nlohmann::json; |
| 17 | + |
| 18 | +// // Sets default values for this component's properties |
| 19 | +UGeoJsonStructGeneratorComponent::UGeoJsonStructGeneratorComponent() |
| 20 | +{ |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +// void tokenize(string &str, char delim, vector<string> &out) |
| 25 | +// { |
| 26 | +// size_t start; |
| 27 | +// size_t end = 0; |
| 28 | +// |
| 29 | +// while ((start = str.find_first_not_of(delim, end)) != string::npos) |
| 30 | +// { |
| 31 | +// end = str.find(delim, start); |
| 32 | +// out.push_back(str.substr(start, end - start)); |
| 33 | +// } |
| 34 | +// } |
| 35 | + |
| 36 | +void UGeoJsonStructGeneratorComponent::OpenMyFileDialog(const FString& DialogTitle, const FString& DefaultPath, |
| 37 | + const FString& FileTypes, TArray<FString>& OutFileNames) |
| 38 | +{ |
| 39 | + if (GEngine) |
| 40 | + |
| 41 | + { |
| 42 | + //void* ParentWindowHandle = GEngine->GameViewport->GetWindow()->GetNativeWindow()->GetOSWindowHandle(); |
| 43 | + IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); |
| 44 | + if (DesktopPlatform) |
| 45 | + { |
| 46 | + //Opening the file picker! |
| 47 | + uint32 SelectionFlag = 0; |
| 48 | + //A value of 0 represents single file selection while a value of 1 represents multiple file selection |
| 49 | + DesktopPlatform->OpenFileDialog(NULL, DialogTitle, DefaultPath, FString(""), FileTypes, SelectionFlag, |
| 50 | + OutFileNames); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +FString UGeoJsonStructGeneratorComponent::ParseJsonToString(FString FilePath, FString FileName) |
| 57 | +{ |
| 58 | + |
| 59 | + FilePath += + "\\" + FileName; |
| 60 | + FString JsonString; //Json converted to FString |
| 61 | + FFileHelper::LoadFileToString(JsonString, *FilePath); |
| 62 | + return JsonString; |
| 63 | + //std::string MyStdString(TCHAR_TO_UTF8(*JsonString)); |
| 64 | +} |
| 65 | +// |
| 66 | +// void UGeoJsonStructGeneratorComponent::ConvertLongLatToXY(FJsonValueArray arry) |
| 67 | +// { |
| 68 | +// auto test = arry; |
| 69 | +// |
| 70 | +// return test; |
| 71 | +// } |
| 72 | + |
| 73 | + |
| 74 | +// void from_json(const nlohmann::json& object, Inner& i) |
| 75 | +// { |
| 76 | +// // j.at("Name").get_to(i.Name); |
| 77 | +// // j.at("Value").get_to(i.Value); |
| 78 | +// |
| 79 | +// if (object.contains("type")) |
| 80 | +// { |
| 81 | +// object.at("type").get_to(i.Value); |
| 82 | +// } |
| 83 | +// object.at("geometry")["type"].get_to(i.Value); |
| 84 | +// } |
| 85 | + |
| 86 | + |
| 87 | +void UGeoJsonStructGeneratorComponent::ParseJson(const FString& File, TArray<FGeoJsonStruct>& GeoJsonStructs) |
| 88 | +{ |
| 89 | + // json j; |
| 90 | + // ifstream ifs(*File); |
| 91 | + // if (!ifs.is_open()) |
| 92 | + // { |
| 93 | + // //return false;; |
| 94 | + // } |
| 95 | + // ifs >> j; |
| 96 | + // ifs.close(); |
| 97 | + |
| 98 | + |
| 99 | + // // iterate the array |
| 100 | + // for (json::iterator it = j.begin(); it != j.end(); ++it) { |
| 101 | + // std::cout << *it << '\n'; |
| 102 | + // } |
| 103 | + // |
| 104 | + // // range-based for |
| 105 | + // for (auto& element : j) { |
| 106 | + // std::cout << element << '\n'; |
| 107 | + // } |
| 108 | + |
| 109 | + |
| 110 | +// for (auto& elm : j.items()) |
| 111 | +// { |
| 112 | +// nlohmann::json object = elm.value(); |
| 113 | +// |
| 114 | +// string type; |
| 115 | +// int32 id; |
| 116 | +// string sourceLayer; |
| 117 | +// string source; |
| 118 | +// |
| 119 | +// string Geotype = object.at("geometry")["type"]; |
| 120 | +// |
| 121 | +// nlohmann::json GeoArrycoordinates = object.at("geometry")["coordinates"]; |
| 122 | +// |
| 123 | +// if (object.contains("type")) |
| 124 | +// { |
| 125 | +// type = object.at("type"); |
| 126 | +// } |
| 127 | +// nlohmann::json Objproperties = object.at("properties"); |
| 128 | +// |
| 129 | +// if (object.contains("id")) |
| 130 | +// { |
| 131 | +// id = object.at("id"); |
| 132 | +// } |
| 133 | +// |
| 134 | +// nlohmann::json Objlayer = object.at("layer"); |
| 135 | +// |
| 136 | +// if (object.contains("sourceLayer")) |
| 137 | +// { |
| 138 | +// sourceLayer = object.at("sourceLayer"); |
| 139 | +// } |
| 140 | +// |
| 141 | +// if (object.contains("source")) |
| 142 | +// { |
| 143 | +// source = object.at("source"); |
| 144 | +// } |
| 145 | +// |
| 146 | +// nlohmann::json Objstate = object.at("state"); |
| 147 | +// |
| 148 | +// |
| 149 | +// if (Geotype == "LineString") |
| 150 | +// { |
| 151 | +// // JsonCoordinatesArray = Coordinates[1]->AsArray(); |
| 152 | +// } |
| 153 | +// |
| 154 | +// |
| 155 | +// |
| 156 | +// |
| 157 | +// |
| 158 | +// |
| 159 | +// |
| 160 | +// |
| 161 | +// // FString Geotype(SGeotype. c_str()); |
| 162 | +// // FString type(Stype.c_str()); |
| 163 | +// // |
| 164 | +// // FGeoJsonStruct GeoJsonStruct = FGeoJsonStruct::BuildGeoJsonStruct( |
| 165 | +// // Geotype, type |
| 166 | +// // ); |
| 167 | +// // |
| 168 | +// // GeoJsonStructs.Push(GeoJsonStruct); |
| 169 | +// // from_json(); |
| 170 | +// // FString Geotype(SGeotype.c_str()); |
| 171 | +// // FString type(Stype.c_str()); |
| 172 | +// // |
| 173 | +// // |
| 174 | +// // FGeoJsonStruct GeoJsonStruct = FGeoJsonStruct::BuildGeoJsonStruct( |
| 175 | +// // Geotype, type |
| 176 | +// // ); |
| 177 | +// // |
| 178 | +// // GeoJsonStructs.Push(GeoJsonStruct); |
| 179 | +// //std::cout << id << std::endl; |
| 180 | +// // GLog->Log("multiline done"); |
| 181 | +// } |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +// |
| 186 | +// void UGeoJsonStructGeneratorComponent::GenerateStructsFromJson(const FString& File, |
| 187 | +// TArray<FGeoJsonStruct>& GeoJsonStructs) |
| 188 | +// { |
| 189 | +// FString JsonString; //Json converted to FString |
| 190 | +// FFileHelper::LoadFileToString(JsonString, *File);; |
| 191 | +// |
| 192 | +// // char* path = TCHAR_TO_ANSI(*File); |
| 193 | +// // |
| 194 | +// // string s=path; |
| 195 | +// // char d='-'; |
| 196 | +// // vector<string> a; |
| 197 | +// // tokenize(s,d,a); |
| 198 | +// // string & zoom = a.at(1); |
| 199 | +// // string & resolution = a.at(4); |
| 200 | +// // GLog->Log(zoom.c_str()); |
| 201 | +// // GLog->Log(resolution.c_str()); |
| 202 | +// |
| 203 | +// |
| 204 | +// TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(*JsonString); |
| 205 | +// TArray<TSharedPtr<FJsonValue>> RawJsonValueArray; |
| 206 | +// bool success = FJsonSerializer::Deserialize(JsonReader, RawJsonValueArray); |
| 207 | +// |
| 208 | +// |
| 209 | +// if (success) |
| 210 | +// { |
| 211 | +// for (int32 Index = 0; Index < RawJsonValueArray.Num(); Index++) |
| 212 | +// { |
| 213 | +// TSharedPtr<FJsonObject>* JsonObject = new TSharedPtr<FJsonObject>(RawJsonValueArray[Index]->AsObject()); |
| 214 | +// FJsonObject* GeoJsonObject = JsonObject->Get(); |
| 215 | +// FString GeometryType; |
| 216 | +// FString Type; |
| 217 | +// TArray<TSharedPtr<FJsonValue>> JsonCoordinatesArray; |
| 218 | +// // FVector GeometryLongLat; |
| 219 | +// |
| 220 | +// // TSharedPtr<FJsonValue> GeoJsonValue = RawJsonValueArray[Index]; |
| 221 | +// // TSharedPtr<FJsonObject> GeoJsonObject = GeoJsonValue->AsObject(); |
| 222 | +// |
| 223 | +// if (GeoJsonObject->HasField("geometry")) |
| 224 | +// { |
| 225 | +// TSharedPtr<FJsonObject> geometry = GeoJsonObject->GetObjectField("geometry"); |
| 226 | +// |
| 227 | +// if (GeoJsonObject->HasField("type")) |
| 228 | +// { |
| 229 | +// Type = GeoJsonObject->GetStringField("type"); |
| 230 | +// } |
| 231 | +// if (geometry->HasField("type")) |
| 232 | +// { |
| 233 | +// GeometryType = geometry->GetStringField("type"); |
| 234 | +// } |
| 235 | +// |
| 236 | +// |
| 237 | +// TArray<TSharedPtr<FJsonValue>> Coordinates = geometry->GetArrayField("coordinates"); |
| 238 | +// if (GeometryType == "MultiLineString") |
| 239 | +// { |
| 240 | +// JsonCoordinatesArray = Coordinates[0]->AsArray(); |
| 241 | +// } |
| 242 | +// if (GeometryType == "LineString") |
| 243 | +// { |
| 244 | +// JsonCoordinatesArray = Coordinates; |
| 245 | +// } |
| 246 | +// |
| 247 | +// if (GeometryType == "LineString") |
| 248 | +// { |
| 249 | +// JsonCoordinatesArray = Coordinates[1]->AsArray(); |
| 250 | +// } |
| 251 | +// double Long; |
| 252 | +// double Lat; |
| 253 | +// TArray<FVector> GeometryCoordinates; |
| 254 | +// |
| 255 | +// for (int32 i = 0; i < JsonCoordinatesArray.Num(); i++) |
| 256 | +// { |
| 257 | +// TArray<TSharedPtr<FJsonValue>> JsonLongLat = JsonCoordinatesArray[i]->AsArray(); |
| 258 | +// Long = JsonLongLat[0]->AsNumber(); |
| 259 | +// Lat = JsonLongLat[1]->AsNumber(); |
| 260 | +// |
| 261 | +// FVector LonLatCoordinates; |
| 262 | +// |
| 263 | +// LonLatCoordinates.X = Long; |
| 264 | +// LonLatCoordinates.Y = Lat; |
| 265 | +// |
| 266 | +// GeometryCoordinates.Push(LonLatCoordinates); |
| 267 | +// |
| 268 | +// FGeoJsonStruct GeoJsonStruct = FGeoJsonStruct::BuildGeoJsonStruct( |
| 269 | +// GeometryType, Type, GeometryCoordinates |
| 270 | +// ); |
| 271 | +// |
| 272 | +// GeoJsonStructs.Push(GeoJsonStruct); |
| 273 | +// |
| 274 | +// |
| 275 | +// // |
| 276 | +// // if (GeoJsonObject->HasField("properties")) |
| 277 | +// // { |
| 278 | +// // TSharedPtr<FJsonObject> properties = GeoJsonObject->GetObjectField("properties");; |
| 279 | +// // } |
| 280 | +// // if (GeoJsonObject->HasField("layer")) |
| 281 | +// // { |
| 282 | +// // TSharedPtr<FJsonObject> layer = GeoJsonObject->GetObjectField("layer"); |
| 283 | +// // } |
| 284 | +// // if (GeoJsonObject->HasField("source")) |
| 285 | +// // { |
| 286 | +// // FString source = GeoJsonObject->GetStringField("source"); |
| 287 | +// // } |
| 288 | +// // if (GeoJsonObject->HasField("id")) |
| 289 | +// // { |
| 290 | +// // FString source = GeoJsonObject->GetStringField("id"); |
| 291 | +// // } |
| 292 | +// // if (GeoJsonObject->HasField("state")) |
| 293 | +// // { |
| 294 | +// // TSharedPtr<FJsonObject> state = GeoJsonObject->GetObjectField("state"); |
| 295 | +// // } |
| 296 | +// } |
| 297 | +// } |
| 298 | +// } |
| 299 | +// } |
| 300 | +// } |
0 commit comments