question

MPeyman avatar image
0 Likes"
MPeyman asked Kavika F commented

creating GIS points from csv file with script

Hi, I try to write code in which instead of creating the several points, just read all the pints in my csv file and add it to the GIS map but I get an error. Also I would like to connect all points:

// Create a map object if one does not exist

Object map = model().find("BCNMap");

if (!map) {

map = createinstance(library().find("/GIS/Map"), model());

map.name = "BCNMap";

map.setSize(30, 20, 2);

map.setLocation(10, 0, 0);

map.setProperty("Latitude", 41.3979379766732);

map.setProperty("Longitude", 2.17978500482735);

map.setProperty("Zoom", 15.742556607617);

}

// The map should have asserted a GISNavigator when it was created

Object navigator = model().find("GISNavigator");

if (!navigator)

return 0;

Object map = model().find("BCNMap");


// Define the file path to the CSV file.

string filePath = "C:\Users\points.csv";


// Read the entire file into a string

string file = readfile(filePath);


// Split the string into an array of lines

string[] lines = split(file, "\n");


//get the GIS navigator from the model

Object navigator = model().find("GISNavigator");

if (!navigator)

return 0;


//Delete existing points

navigator.subnodes.clear();


//loop through the lines of the csv file

for (int i = 1; i < size(lines); i++) {

//split the line into an array of values

string[] values = split(lines[i], ",");

//Get the lat and long values from the current line.

double lat = toDouble(values[0]);

double lon = toDouble(values[1]);

//Create a new point object and set the lat and long values.

Object point = createinstance(library().find("/GIS/Point"), navigator);

point.setProperty("Latitude", lat);

point.setProperty("Longitude", lon);

FlexSim 23.0.0
gislogistics
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Kavika F avatar image
0 Likes"
Kavika F answered Kavika F commented

Hey @MPeyman, if you save your points from your csv file to a Global Table, you can programmatically create your points with their proper latitude and longitude. Here's an example I did.

GIS_Coded_Points.fsm

First I made a Global Table with some fake points.

1673461962478.png

I added a GIS Map to my 3D Model. Then I wrote a simple script that, when executed, will look at the coordinates in my table and set the properties of that point.

1673462015240.png

Here's what it looks like in action:

gis-points-generated.gif


· 5
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

MPeyman avatar image MPeyman commented ·

Hi Dear @Kavika F , Awsome, it was helpful, thank you so much. according to your code I wrote this code to connect each node but it give me an error of "too many parameters", do you know what is missing?:

// Connect the points to create a route

for (int i = 1; i <= points.numRows; i++) {

double lat1 = points[i][1];

double long1 = points[i][2];

double lat2 = points[i+1][1];

double long2 = points[i+1][2];

Object point1 = Object.find("Point", lat1, long1);

Object point2 = Object.find("Point", lat2, long2);

contextdragconnection(point1, point2, "A");

0 Likes 0 ·
Joerg Vogel avatar image Joerg Vogel MPeyman commented ·

find method expects just one string parameter not three!

Please do not anticipate something while you write codes. Instead read the references of scripting language.


1 Like 1 ·
Joerg Vogel avatar image Joerg Vogel commented ·

hello @Kavika F, following source code line seems to be important for creating new points.

Object point = Object.create(“GIS::Point”);

What exactly does “GIS::Point” do? Is it a name? Is it a constructor? I have seen similar structures in programming languages but I am still only an experienced user and not a developer at SDK level.
How would I get a reference to this new created point? Am I allowed to give the treenode a unique name? Thank you. Regards Jörg

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦ Joerg Vogel commented ·

It's just a reference to the "Point" class using the namespace "GIS". I think the convention in FlexSim is that the module name is the namespace with the "::" scope resolution operator telling the interpreter where to find the Point class. . Created points are subnodes of the GIS navigator, but still FlexSim objects, so in the example 'point' is the local variable you use as a reference and point.name="NewName" will rename it as normal. The routes are couplings between the two points with extra SDT data.

1 Like 1 ·
Kavika F avatar image Kavika F ♦ Joerg Vogel commented ·
Yes, like Jason said, "Point" is a class within the "GIS" namespace. Object.create() was added in 22.2.0. If you want to create an object, you have to do so in the format ("[module::]class") where "class" is the name of the class of the object you want to create and "module::" is only required if that class resides in a specific module. If you start typing "Object.create(" into a script window, it will auto-populate a list with all the possible objects you can create along with their preceding module names.
1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.