Closed yuanhuihui1203 closed 2 months ago
Hi @yuanhuihui1203,
The turf.bbox
documentation specifies that the first argument must be a GeoJSON Object.
In your first image above, the argument is a string – the return value from JSON.stringify
– so turf.bbox
fails.
In your second image, the argument is apparently a .NET dictionary, which turf.bbox
also doesn't recognize.
We aren't familiar with Turf.js, but it looks like the argument to that method must be a native JavaScript object. Do you need help with constructing one?
Of course, I am willing to do so
This is turf.js CDN :https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js
This is my demo:
try { using (var Engine = new V8ScriptEngine()) { var file_str = Environment.CurrentDirectory + @"\lib\turf.min.js";
if (!File.Exists(file_str))
throw new FileNotFoundException("path is not found...");
Engine.Execute(File.ReadAllText(file_str));
// var data = @"
// {
// ""type"":""Polygon"",
// ""coordinates"":[
// [
// [""108.3772586426722"",""22.811010986889986""],
// [""108.37827566589279"",""22.815062279607343""],
// [""108.37754916500549"",""22.815444906122217""],
// [""108.37642578209547"",""22.81427829606639""],
// [""108.3754340489081"",""22.811905891718265""],
// [""108.37544099548577"",""22.811001924694395""],
// [""108.3750624387526"",""22.807459337499285""],
// [""108.3750624387526"",""22.807459337499285""],
// [""108.37684873041493"",""22.80836068823839""],
// [""108.3772586426722"",""22.811010986889986""]]]}
// ";
var dictData = new Dictionary<string, object>
{
{ "type", "Polygon" },
{
"coordinates", new[,,]
{
{
{ 108.3772586426722, 22.811010986889986 },
{ 108.37827566589279, 22.815062279607343 },
{ 108.37754916500549, 22.815444906122217 },
{ 108.37642578209547, 22.81427829606639 },
{ 108.3754340489081, 22.811905891718265 },
{ 108.37544099548577, 22.811001924694395 },
{ 108.3750624387526, 22.807459337499285 },
{ 108.3750624387526, 22.807459337499285 },
{ 108.37684873041493, 22.80836068823839 },
{ 108.3772586426722, 22.811010986889986 },
}
}
}
};
var res = Engine.Script.turf.bbox(dictData);
Console.WriteLine("result:>{0}", res);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
The final result is this error:Error: Unknown Geometry Type
Hi @yuanhuihui1203,
Again, you're creating a .NET dictionary and .NET arrays, whereas turf.bbox
requires a JavaScript object and JavaScript arrays.
With a few exceptions, ClearScript does not convert between .NET and JavaScript objects and arrays, as that sort of conversion is lossy. Instead, it aims to make both .NET and JavaScript objects and arrays easily accessible from either side.
In this case, however, you need to construct a JavaScript input object for turf.bbox
. To do that from .NET, you'll need a bit of help from the script engine. Consider something like this:
dynamic newObject = Engine.Evaluate("() => ({})");
dynamic newArray = Engine.Evaluate("(function () { return [...arguments]; })");
You now have all the tools you need to build the turf.bbox
input object from .NET:
var data = newObject();
data.type = "Polygon";
data.coordinates = newArray(newArray(
newArray(108.3772586426722, 22.811010986889986),
newArray(108.37827566589279, 22.815062279607343),
newArray(108.37754916500549, 22.815444906122217),
newArray(108.37642578209547, 22.81427829606639),
newArray(108.3754340489081, 22.811905891718265),
newArray(108.37544099548577, 22.811001924694395),
newArray(108.3750624387526, 22.807459337499285),
newArray(108.3750624387526, 22.807459337499285),
newArray(108.37684873041493, 22.80836068823839),
newArray(108.3772586426722, 22.811010986889986)
));
var res = Engine.Script.turf.bbox(data);
Console.WriteLine("result:> {0}", res.toString());
Good luck!
I think this gives me a good example. I need to try following this idea. Thank you very much
No problem at all. Please reopen this issue if you have additional questions about this topic. Thank you!
I am using a great JS library called Turf for processing spatial information. When using the Turf.bbox method, I found that ClearScript seems to have undergone JSON serialization when passing parameters? I don't know if I'm a beginner in C # or ClearScript, but this library is great and very similar to the one I use in Golang for its convenience. However, I found that when using Turf.bbox, if the parameters passed in are JSON. stringfy, an error will occur. I don't know how to handle it in ClearScript to make my parameters pass in to Turf like a normal object Just like this:
This is my code:
I have been searching for examples for a long time and have seen many proposals. Their problems are all great, but they cannot solve my problem. I hope to get your help tks!