I have the following code in NetLogo and I am trying to convert the variables lat and long to numbers because setxy accepts only numbers:
to go
if file-at-end? [ stop ]
set data csv:from-row file-read-line
let lat item 1 data
let lng item 2 data
move-turt lat lng
tick
end
to move-turt [lat lng]
print lat
ask turtles[setxy lat lng]
end
I tried to use read-from-strings but it didn't work and I couldn't find anything in the dictionary.
Any ideas?
Thanks
Related
I am using PostGIS to calculate length of a user-defined linestring. The column is defined as geography(LineString,4326).
The linestring is represented by this GeoJSON:
"track": {
"type": "LineString",
"coordinates": [
[
49.364325571013,
16.785549033597
],
[
49.363254969491,
16.642149334451
]
]
}
SELECT ST_Length("geography") FROM table; returns 15945.7486086962 but the length measured on Google Maps is ~10 km.
What am I doing wrong? How to measure the length to get the same value as from Google Maps?
I believe it is the classic issue of switching x,y positions.
Considering x,y:
SELECT
ST_Length(
ST_GeogFromText('SRID=4326;LINESTRING(49.364325571013 16.785549033597,49.363254969491 16.642149334451)'),true);
st_length
------------------
15869.9069442778
and the "same" LineString switching to y,x ..
SELECT
ST_Length(
ST_GeogFromText('SRID=4326;LINESTRING(16.785549033597 49.364325571013,16.642149334451 49.363254969491)'),true)
st_length
------------------
10416.8606521809
Figured out the problem.
I was using ST_GeomFromText('LINESTRING(lat lon, lat lon)') to create the line. The correct order is lon lat, so ST_GeomFromText('LINESTRING(lon lat, lon lat)').
GeoJSON uses the same order of coordinates: 49.363254969491 = latitude, 16.642149334451 = longitude.
The reason I didn't realize this was because I used Leaflet to draw the line. I basically took the points by geoJSON.getLayers()[0].feature.geometry.coordinates and passed them to a Polyline object. It created the Polyline points by taking the first coordinate from the GeoJSON as Lat, the second as Lng. This way it got reversed the second time and got rendered correctly.
So after switching lon lat in the query I had to use L.GeoJSON.coordsToLatLngs() function to correctly render the line.
I have a netlogo gis model consisting of a road network. The .shp file has an attribute length which measures the distance of the roads. The polyline making up the roads in the .shp file are exploded, so that distance between each vertex can be found.
Instead of using netlogo to calculate the distance ( I presume it uses the coordinates to calculate distance between vertices), which will result the distance values in netlogo units. I am wondering if its possible to copy the length attribute from the gis file to the corresponding links? This will give the actual distances in the roads
Any ideas how to do this?
Thanks
In the netlogo model I have looped over each of the feature list, copied the "distance" from the .shp file. Then looped over the vertices, created the vertices (nodes).Then formed a linked between previous and current vertex (node).
i.e node/vertex list: vertex 137 vertex 141 vertex 150 vertex 205 ....
i.e link list: link 137 141 (end1: vertex 137 end2: vertex 141) , link 150 205 (end1: vertex 150 end2: vertex 205) ...
The idea is to have some variable like distance-in-meters as a links-owns variable. This variable will store the distance value of the link.
breed [vertices vertex]
links-own [distance_in_meters]
foreach gis:feature-lists-of roads[
i ->
set dist_meters gis:property-value i "LENGTH"
foreach gis:vertex-list-of i[
j ->
let first-node nobody
let previous-node nobody
foreach j[
k ->
let location gis:location-of k
if not empty? location[
ifelse any? vertices with [xcor = item 0 location and ycor = item 1
location]
[]
[
create-vertices 1[
set xcor item 0 location
set ycor item 1 location
set shape "circle"
set size 0.2
]
]
let node-here (vertices with [xcor = item 0 location and ycor = item 1
location]
if-else previous-node-point = nobody
[set first-node node-here]
[let who-node 0
let who-prev 0
as node-here
[create-link-with previous-node
set who-node who]
ask previous-node-point [set who-prev who]
]
set previous-node one-of nodes-here
]]]]
I need some help. My issue is the following
I want to solve the following formula
sum (Zi - Zj)^2 where Zi is a constant for an individual i and Zj is the value for a generic individual j that is within a neighborood with radius = 1 of the individual i.
Therefore, I want a sum of the square of the distance between a constant value and the value of Z for each individual within that radius.
Sorry for the absence of my code, but I have no idea about how to approach this issue
I will write an example
Zi = 1
The neighborhood of the individual i is composed of 2 agents, let say a and b where Za = 3 and Zb = 5
I want the following result
(1-3)^2 + (1-5)^2 = 20
Thanks
I think you want to do the sum of squared differences between a constant and a list of numbers, where the list of numbers is the value of Z for several turtles. If this is correct, then the following is a complete model that does what you want.
turtles-own [ varZ ]
to setup
clear-all
create-turtles 40
[ setxy random-xcor random-ycor
set varZ random 10
set color blue
]
testme
end
to testme
ask one-of turtles
[ set color red
let friends other turtles in-radius 4
ask friends [ set color yellow ]
type "my varZ is: " print varZ
type "sum of squared differences is: " print sum-sq-diff varZ [varZ] of friends
]
end
to-report sum-sq-diff [#constant #listvals]
report reduce + (map [ thisval -> (thisval - #constant) ^ 2 ] #listvals)
end
The procedure sum-sq-diff takes two inputs: a constant and a list of values. It calculates the squared sum of differences between the constant and each value in the list. The map does the square of differences and creates a list of those values, then the reduce sums across the list. You can test is by simply typing sum-sq-diff 1 [ 2 3 4 ] in the command centre and you will get back 14 (which is (2-1)^2 + (3-1)^2 + (4-1)^2).
The rest of the code is an example of how to use this procedure in the context I think you want, pulling out the turtles within some radius and using their variable values as the list.
I have sorted all the turtles by the value of a property called point.
now I want a plot of point versus turtle number. how do I do this?
turtles-own [ point ]
to setup
ca
crt 100
reset-ticks
end
to go
repeat 100[
ask turtles[
if random 10 = 1[
set point point + 1
]
]
];;sorting
let array sort-on [point] turtles
tick
end
By "turtle number", I assume you mean the location in the list. Then replace let array sort-on [point] turtles with plotByOrder where
to plotByOrder
clear-plot
let pts sort [point] of turtles
foreach pts [[pt] -> plot pt]
end
Of course, you will need to have created a plot in the interface, and this assumes it is the current plot.
I am working on this script and need to query the data object that I get through netCDF4 (first example) to retrieve values at a specific latitude and longitude coordinate. I am not sure how to index the data object in the example with lat/long in degrees or how to map coordinates onto a meshgrid and query from there. Ideas anyone?
Based on the NOAA sample script and with the help of the ww3 mail list, I figured this:
In order to get values from the data object, you do
from mpl_toolkits.basemap import Basemap,interp
lat = GPSLat
lon = (360 + GPSLon)
value = interp(data, lon, lat, np.asarray( [[ (360+ GPSLon) % 360 ]] ), np.asarray( [[ GPSLat ]] ), checkbounds=True, masked=True, order=1)