NetLogo / GIS-Extension

the GIS (Geographic Information Systems) extension bundled with NetLogo
Other
27 stars 20 forks source link

`gis:create-turtles-inside-polygon` fails with `Extension exception: Index 0 out of bounds for length 0` #44

Open EwoutH opened 1 year ago

EwoutH commented 1 year ago

I'm using the gis:create-turtles-inside-polygon function with the GIS Extension of NetLogo 6.3.0. I follow the example listed in the docs, with a few lines of code added to place each agent in the center of that polygon. That also prevents all agents outside my world envelope from being created.

However, it fails with a weird error: Extension exception: Index 0 out of bounds for length 0. I tried about everything, but it just keeps failing. Could it be my data source?

@JamesHovet, since you wrote this function, do you have any idea what's going on? Or @LaCuneta?

Here are my model and data: NetLogo-GIS-test.zip

Model code ```NetLogo extensions [gis] globals [parking-dataset projection] breed [spots spot] spots-own [AMENITY] to setup clear-all load draw setup-spots end to load set projection "WGS_84_Geographic" set parking-dataset gis:load-dataset "export2.geojson" gis:set-world-envelope (list 5.671533 5.684606 52.026888 52.019735) end to setup-spots ;; For each parking space, determine the centroid, convert to x,y coordinates (list with 2 elements), ;; and create a turtle in the center of that polygon foreach gis:feature-list-of parking-dataset [ this-vector-feature -> let center gis:centroid-of this-vector-feature let loc gis:location-of center if length loc >= 2 [ gis:create-turtles-inside-polygon this-vector-feature spots 1;; [setxy item 0 loc item 1 loc] ] ] end to draw gis:set-drawing-color red gis:fill parking-dataset 0 end to clear clear-all end ```
Full error > Extension exception: Index 0 out of bounds for length 0 > error while observer running GIS:CREATE-TURTLES-INSIDE-POLYGON > called by (anonymous command: [ this-vector-feature -> let center gis:centroid-of this-vector-feature let loc gis:location-of center if length loc >= 2 [ gis:create-turtles-inside-polygon this-vector-feature spots 1 ] ]) > called by procedure SETUP-SPOTS > called by procedure SETUP > called by Button 'setup' > > org.nlogo.nvm.WrappedExtensionException: Extension exception: Index 0 out of bounds for length 0 > at org.nlogo.nvm.AnonymousCommand.perform(AnonymousProcedure.scala:212) > at org.nlogo.nvm.AnonymousCommand.perform(AnonymousProcedure.scala:184) > at org.nlogo.prim.etc._foreach.perform(_foreach.scala:38) > at org.nlogo.nvm.Context.stepConcurrent(Context.java:107) > at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.scala:65) > at org.nlogo.job.JobThread.runPrimaryJobs(JobThread.scala:133) > at org.nlogo.job.JobThread.$anonfun$run$1(JobThread.scala:68) > at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) > at scala.util.control.Exception$Catch.apply(Exception.scala:228) > at org.nlogo.api.Exceptions$.handling(Exceptions.scala:41) > at org.nlogo.job.JobThread.run(JobThread.scala:66) > Caused by: org.nlogo.api.ExtensionException: Index 0 out of bounds for length 0 > at com.vividsolutions.jts.geom.GeometryCollection.getGeometryN(GeometryCollection.java:138) > at org.myworldgis.netlogo.VectorFeature.getRandomTriangleWeightedByArea(VectorFeature.java:347) > at org.myworldgis.netlogo.VectorFeature.getRandomPointInsidePolygon(VectorFeature.java:359) > at org.myworldgis.netlogo.CreateTurtlesInsidePolygon$TurtlesInsidePolygon.performInternal(CreateTurtlesInsidePolygon.java:63) > at org.myworldgis.netlogo.CreateTurtlesInsidePolygon$TurtlesInsidePolygonAutomatic.performInternal(CreateTurtlesInsidePolygon.java:83) > at org.myworldgis.netlogo.GISExtension$Command.perform(GISExtension.java:61) > at org.nlogo.prim._extern.perform(_extern.java:36) > at org.nlogo.nvm.Context.runExclusive(Context.java:133) > at org.nlogo.nvm.AnonymousCommand.perform(AnonymousProcedure.scala:206) > ... 10 more > > NetLogo 6.3.0 > main: org.nlogo.app.AppFrame > thread: JobThread > OpenJDK 64-Bit Server VM 17.0.3 (BellSoft; 17.0.3+7-LTS) > operating system: Windows 11 10.0 (amd64 processor) > Scala version 2.12.16 > JOGL: (3D View not initialized) > OpenGL Graphics: (3D View not initialized) > model: GIS_test > > 01:17:21.163 RuntimeErrorEvent (org.nlogo.app.App$$anon$4 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 > 01:17:21.147 JobRemovedEvent (org.nlogo.app.App$$anon$4 (org.nlogo.window.GUIWorkspace)) JobThread > 01:17:21.147 OutputEvent (org.nlogo.app.App$$anon$4 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 > 01:17:21.139 PeriodicUpdateEvent (org.nlogo.app.App$$anon$4 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 > 01:17:21.139 TickStateChangeEvent (org.nlogo.app.App$$anon$4 (org.nlogo.window.GUIWorkspace)) JobThread > 01:17:21.139 OutputEvent (org.nlogo.app.App$$anon$4 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 > 01:17:21.139 AddJobEvent (org.nlogo.window.ButtonWidget) AWT-EventQueue-0 > 01:17:21.089 InputBoxLoseFocusEvent (org.nlogo.window.ButtonWidget) AWT-EventQueue-0 > 01:17:21.038 PeriodicUpdateEvent (org.nlogo.app.App$$anon$4 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 > 01:17:20.823 PeriodicUpdateEvent (org.nlogo.app.App$$anon$4 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0
JamesHovet commented 1 year ago

Looks like there's something in that dataset that is giving gis:create-turtles-inside-polygon a hard time. The good news for you @EwoutH is that since you want the turtles you are creating to be in the center of the polygon, the old fashioned way of creating turtles from polygons (before I created gis:create-turtles-inside-polygon) still works for you! Try this for your setup-spots function:

to setup-spots
  ;; For each parking space, determine the centroid, convert to x,y coordinates (list with 2 elements),
  ;; and create a turtle in the center of that polygon
  foreach gis:feature-list-of parking-dataset [ this-vector-feature ->
    let center gis:centroid-of this-vector-feature
    let loc gis:location-of center

    if length loc >= 2 [
      create-spots 1 [
        setxy item 0 loc item 1 loc
        set AMENITY gis:property-value this-vector-feature "AMENITY"
      ]
    ]
  ]
end
EwoutH commented 1 year ago

Thanks for getting back! It's a bit less scalable when adding other property-values in the future, but in my case I can get it to work.

I generated my dataset using OpenStreetMap data collected on https://overpass-turbo.eu/, using the query:

[bbox:{{bbox}}];

area
  [amenity=parking];
/*added by auto repair*/
(._;>;);
/*end of auto repair*/

out;

In the case anyone wants to debug this.