randomly populating an irregularly shaped shapefile

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

randomly populating an irregularly shaped shapefile

Grant, Tyler J [NREM]

Greetings,

 

I’m working with some shapefiles that are all sorts of shapes and I need to randomly populate them with monarch agents.  My plan was to choose random X and Y coordinates from a rectangle that encompasses the shapefile, then check each random coordinate as to whether it falls in the shapefile or not, and keep or discard it as appropriate.  Repeat until the shapefile is populated. 

 

The problem has been in how to check if the coordinate falls in the shapefile.  If anyone has a better idea of how to check, I would be glad to hear it, but in the meantime I am trying to just get something that works.  So, I have been trying to used GeographyWithin and checking if any polygons are within 0 or 0.001 meters of the coordinate.  However, for some reason GeographyWithin isn’t working in the Context Creator like it works in the GIS Agent class. 

 

So the code to generate the agents looks like this:

 

             //  Generate agents at random initial location

             for (int i = 0; i < numAgents; i++){

                    Monarch monarch = new Monarch("M" + i);//, Monarch.initeggstolay);

                    context.add(monarch);

                    boolean testboolean = false;

                    while (testboolean == false) {

                          

                    //test coordinates for new random agent generator - coords are well outside farm progress boundaries

                    Coordinate coord = new Coordinate(-93.759 - 0.094 * Math.random(),

                                 42.013 + 0.055 * Math.random());

                   

                    //make a list of probEggs for polygons within 0m distance of coords

                    //and if there is none, choose a new coord

                    ArrayList probs = new ArrayList();

                    //Context context = ContextUtils.getContext(this);

                    //Geography<Object> geography = (Geography)context.getProjection("Monarchs");

                   

                    GeographyWithin test = new GeographyWithin(geography, 0.001, coord);

                   

                    for (Object obj : test.query()) {

                           if (obj instanceof ZoneAgent){

                                 ZoneAgent zoneagent = (ZoneAgent)obj;

                                 double probEggs = zoneagent.getprobEggs();

                                 probs.add(probEggs);

                           }

                    }

                   

                    //if size of probs is 0, then choose another

                    if(probs.isEmpty()){

                           testboolean = false;

                    } else {

                           Point geom = fac.createPoint(coord);

                           geography.move(monarch, geom);

                           testboolean = true;

                    }

                    }

             }

 

 

Here are the first several lines from the error report:

 

ERROR [AWT-EventQueue-0] 08:01:34,926 repast.simphony.ui.RSApplication - Error while initializing simulation

java.lang.NullPointerException

       at repast.simphony.space.gis.UTMFinder.getUTMFor(UTMFinder.java:82)

       at repast.simphony.query.space.gis.GeographyWithin.init(GeographyWithin.java:103)

       at repast.simphony.query.space.gis.GeographyWithin.<init>(GeographyWithin.java:47)

       at monarchs.ContextBuild.build(ContextBuild.java:269)

       at repast.simphony.dataLoader.engine.ClassNameContextBuilder.build(ClassNameContextBuilder.java:41)

 

Line 269 in ContextBuild is GeographyWithin test = new GeographyWithin(geography, 0.001, coord);

 

Thanks,

 

 

Dr. Tyler J. Grant

Postdoctoral Associate

Department of Natural Resource Ecology and Management

Iowa State University

339 Science II

Ames, IA 50011

 


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|

Re: randomly populating an irregularly shaped shapefile

Tatara, Eric R.

I would try using Geometry.intersects(Geometry other)

 

http://www.vividsolutions.com/jts/javadoc/com/vividsolutions/jts/geom/Geometry.html

 

This should be much faster since the within query goes through a lot of projection calculations to get the distance right.  The intersects query is based on a simple 2D topology independent of projection.  I think the logic would be

 

create each Monarch {

                while searching for suitable zone polygon{

                                create random coordinate

                                for each zone agent (from a list of zone agents) {

                                                if zone agent intersects with coordinate{

                                                                move Monarch to coordinate

                                                                break

                                                }

}

}

}

 

eric

 

From: Grant, Tyler J [NREM] [mailto:[hidden email]]
Sent: Monday, July 11, 2016 8:04 AM
To: [hidden email]
Subject: [Repast-interest] randomly populating an irregularly shaped shapefile

 

Greetings,

 

I’m working with some shapefiles that are all sorts of shapes and I need to randomly populate them with monarch agents.  My plan was to choose random X and Y coordinates from a rectangle that encompasses the shapefile, then check each random coordinate as to whether it falls in the shapefile or not, and keep or discard it as appropriate.  Repeat until the shapefile is populated. 

 

The problem has been in how to check if the coordinate falls in the shapefile.  If anyone has a better idea of how to check, I would be glad to hear it, but in the meantime I am trying to just get something that works.  So, I have been trying to used GeographyWithin and checking if any polygons are within 0 or 0.001 meters of the coordinate.  However, for some reason GeographyWithin isn’t working in the Context Creator like it works in the GIS Agent class. 

 

So the code to generate the agents looks like this:

 

             //  Generate agents at random initial location

             for (int i = 0; i < numAgents; i++){

                    Monarch monarch = new Monarch("M" + i);//, Monarch.initeggstolay);

                    context.add(monarch);

                    boolean testboolean = false;

                    while (testboolean == false) {

                          

                    //test coordinates for new random agent generator - coords are well outside farm progress boundaries

                    Coordinate coord = new Coordinate(-93.759 - 0.094 * Math.random(),

                                 42.013 + 0.055 * Math.random());

                   

                    //make a list of probEggs for polygons within 0m distance of coords

                    //and if there is none, choose a new coord

                    ArrayList probs = new ArrayList();

                    //Context context = ContextUtils.getContext(this);

                    //Geography<Object> geography = (Geography)context.getProjection("Monarchs");

                   

                    GeographyWithin test = new GeographyWithin(geography, 0.001, coord);

                   

                    for (Object obj : test.query()) {

                           if (obj instanceof ZoneAgent){

                                 ZoneAgent zoneagent = (ZoneAgent)obj;

                                 double probEggs = zoneagent.getprobEggs();

                                 probs.add(probEggs);

                           }

                    }

                   

                    //if size of probs is 0, then choose another

                    if(probs.isEmpty()){

                           testboolean = false;

                    } else {

                           Point geom = fac.createPoint(coord);

                           geography.move(monarch, geom);

                           testboolean = true;

                    }

                    }

             }

 

 

Here are the first several lines from the error report:

 

ERROR [AWT-EventQueue-0] 08:01:34,926 repast.simphony.ui.RSApplication - Error while initializing simulation

java.lang.NullPointerException

       at repast.simphony.space.gis.UTMFinder.getUTMFor(UTMFinder.java:82)

       at repast.simphony.query.space.gis.GeographyWithin.init(GeographyWithin.java:103)

       at repast.simphony.query.space.gis.GeographyWithin.<init>(GeographyWithin.java:47)

       at monarchs.ContextBuild.build(ContextBuild.java:269)

       at repast.simphony.dataLoader.engine.ClassNameContextBuilder.build(ClassNameContextBuilder.java:41)

 

Line 269 in ContextBuild is GeographyWithin test = new GeographyWithin(geography, 0.001, coord);

 

Thanks,

 

 

Dr. Tyler J. Grant

Postdoctoral Associate

Department of Natural Resource Ecology and Management

Iowa State University

339 Science II

Ames, IA 50011

 


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|

Re: randomly populating an irregularly shaped shapefile

cnietocoria
In reply to this post by Grant, Tyler J [NREM]
Hi Grant, 

I am not sure that I fully get what you want to do. My suggestion is make the polygons of the shapefile agents, then you could get the coordinates of the agent from the context. 

Another thing, your GeographyWithin query is not so precise, I locked into the methods and there is four methods to be implemented, but none of them is using coordinates.What you are using does not have the source object.

Or I could be wrong and in the method that uses (geography, distance, location) as parameters. The location can be interpreted as coordinates, I am not sure. Anyway, if you uses an agent you could ask for the geometries that are inside of the geometries.  

I would check ff the query is not empty, 

If ( test.query != null){
for(Object obj : test.query.iterator()){
do all the things... 

}
}


I hope that helps you. If I wasn't clear let me know. 

Cheers 

Cesar


On Mon, Jul 11, 2016 at 3:03 PM, Grant, Tyler J [NREM] <[hidden email]> wrote:

Greetings,

 

I’m working with some shapefiles that are all sorts of shapes and I need to randomly populate them with monarch agents.  My plan was to choose random X and Y coordinates from a rectangle that encompasses the shapefile, then check each random coordinate as to whether it falls in the shapefile or not, and keep or discard it as appropriate.  Repeat until the shapefile is populated. 

 

The problem has been in how to check if the coordinate falls in the shapefile.  If anyone has a better idea of how to check, I would be glad to hear it, but in the meantime I am trying to just get something that works.  So, I have been trying to used GeographyWithin and checking if any polygons are within 0 or 0.001 meters of the coordinate.  However, for some reason GeographyWithin isn’t working in the Context Creator like it works in the GIS Agent class. 

 

So the code to generate the agents looks like this:

 

             //  Generate agents at random initial location

             for (int i = 0; i < numAgents; i++){

                    Monarch monarch = new Monarch("M" + i);//, Monarch.initeggstolay);

                    context.add(monarch);

                    boolean testboolean = false;

                    while (testboolean == false) {

                          

                    //test coordinates for new random agent generator - coords are well outside farm progress boundaries

                    Coordinate coord = new Coordinate(-93.759 - 0.094 * Math.random(),

                                 42.013 + 0.055 * Math.random());

                   

                    //make a list of probEggs for polygons within 0m distance of coords

                    //and if there is none, choose a new coord

                    ArrayList probs = new ArrayList();

                    //Context context = ContextUtils.getContext(this);

                    //Geography<Object> geography = (Geography)context.getProjection("Monarchs");

                   

                    GeographyWithin test = new GeographyWithin(geography, 0.001, coord);

                   

                    for (Object obj : test.query()) {

                           if (obj instanceof ZoneAgent){

                                 ZoneAgent zoneagent = (ZoneAgent)obj;

                                 double probEggs = zoneagent.getprobEggs();

                                 probs.add(probEggs);

                           }

                    }

                   

                    //if size of probs is 0, then choose another

                    if(probs.isEmpty()){

                           testboolean = false;

                    } else {

                           Point geom = fac.createPoint(coord);

                           geography.move(monarch, geom);

                           testboolean = true;

                    }

                    }

             }

 

 

Here are the first several lines from the error report:

 

ERROR [AWT-EventQueue-0] 08:01:34,926 repast.simphony.ui.RSApplication - Error while initializing simulation

java.lang.NullPointerException

       at repast.simphony.space.gis.UTMFinder.getUTMFor(UTMFinder.java:82)

       at repast.simphony.query.space.gis.GeographyWithin.init(GeographyWithin.java:103)

       at repast.simphony.query.space.gis.GeographyWithin.<init>(GeographyWithin.java:47)

       at monarchs.ContextBuild.build(ContextBuild.java:269)

       at repast.simphony.dataLoader.engine.ClassNameContextBuilder.build(ClassNameContextBuilder.java:41)

 

Line 269 in ContextBuild is GeographyWithin test = new GeographyWithin(geography, 0.001, coord);

 

Thanks,

 

 

Dr. Tyler J. Grant

Postdoctoral Associate

Department of Natural Resource Ecology and Management

Iowa State University

339 Science II

Ames, IA 50011

 


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest




--
--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria
skype: cnietocoria


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|

Re: randomly populating an irregularly shaped shapefile

Grant, Tyler J [NREM]
In reply to this post by Grant, Tyler J [NREM]

Eric,

 

I’m getting to the point where I have 100,000’s of zone agents.  So cycling through that list may take a while.  Which might be a reason to use GeographyWithin or something similar.  With that in mind, do you have any other ideas?

 

But, how do I create a list of all the zone agents if Geometry.intersects() is the only way to do it?

 

Tyler

 

Dr. Tyler J. Grant

Postdoctoral Associate

Department of Natural Resource Ecology and Management

Iowa State University

339 Science II

Ames, IA 50011

 

From: Tatara, Eric R. [mailto:[hidden email]]
Sent: Tuesday, July 12, 2016 2:50 PM
To: Grant, Tyler J [NREM] <[hidden email]>; [hidden email]
Subject: RE: [Repast-interest] randomly populating an irregularly shaped shapefile

 

I would try using Geometry.intersects(Geometry other)

 

http://www.vividsolutions.com/jts/javadoc/com/vividsolutions/jts/geom/Geometry.html

 

This should be much faster since the within query goes through a lot of projection calculations to get the distance right.  The intersects query is based on a simple 2D topology independent of projection.  I think the logic would be

 

create each Monarch {

                while searching for suitable zone polygon{

                                create random coordinate

                                for each zone agent (from a list of zone agents) {

                                                if zone agent intersects with coordinate{

                                                                move Monarch to coordinate

                                                                break

                                                }

}

}

}

 

eric

 

From: Grant, Tyler J [NREM] [[hidden email]]
Sent: Monday, July 11, 2016 8:04 AM
To: [hidden email]
Subject: [Repast-interest] randomly populating an irregularly shaped shapefile

 

Greetings,

 

I’m working with some shapefiles that are all sorts of shapes and I need to randomly populate them with monarch agents.  My plan was to choose random X and Y coordinates from a rectangle that encompasses the shapefile, then check each random coordinate as to whether it falls in the shapefile or not, and keep or discard it as appropriate.  Repeat until the shapefile is populated. 

 

The problem has been in how to check if the coordinate falls in the shapefile.  If anyone has a better idea of how to check, I would be glad to hear it, but in the meantime I am trying to just get something that works.  So, I have been trying to used GeographyWithin and checking if any polygons are within 0 or 0.001 meters of the coordinate.  However, for some reason GeographyWithin isn’t working in the Context Creator like it works in the GIS Agent class. 

 

So the code to generate the agents looks like this:

 

             //  Generate agents at random initial location

             for (int i = 0; i < numAgents; i++){

                    Monarch monarch = new Monarch("M" + i);//, Monarch.initeggstolay);

                    context.add(monarch);

                    boolean testboolean = false;

                    while (testboolean == false) {

                          

                    //test coordinates for new random agent generator - coords are well outside farm progress boundaries

                    Coordinate coord = new Coordinate(-93.759 - 0.094 * Math.random(),

                                 42.013 + 0.055 * Math.random());

                   

                    //make a list of probEggs for polygons within 0m distance of coords

                    //and if there is none, choose a new coord

                    ArrayList probs = new ArrayList();

                    //Context context = ContextUtils.getContext(this);

                    //Geography<Object> geography = (Geography)context.getProjection("Monarchs");

                   

                    GeographyWithin test = new GeographyWithin(geography, 0.001, coord);

                   

                    for (Object obj : test.query()) {

                           if (obj instanceof ZoneAgent){

                                 ZoneAgent zoneagent = (ZoneAgent)obj;

                                 double probEggs = zoneagent.getprobEggs();

                                 probs.add(probEggs);

                           }

                    }

                   

                    //if size of probs is 0, then choose another

                    if(probs.isEmpty()){

                           testboolean = false;

                    } else {

                           Point geom = fac.createPoint(coord);

                           geography.move(monarch, geom);

                           testboolean = true;

                    }

                    }

             }

 

 

Here are the first several lines from the error report:

 

ERROR [AWT-EventQueue-0] 08:01:34,926 repast.simphony.ui.RSApplication - Error while initializing simulation

java.lang.NullPointerException

       at repast.simphony.space.gis.UTMFinder.getUTMFor(UTMFinder.java:82)

       at repast.simphony.query.space.gis.GeographyWithin.init(GeographyWithin.java:103)

       at repast.simphony.query.space.gis.GeographyWithin.<init>(GeographyWithin.java:47)

       at monarchs.ContextBuild.build(ContextBuild.java:269)

       at repast.simphony.dataLoader.engine.ClassNameContextBuilder.build(ClassNameContextBuilder.java:41)

 

Line 269 in ContextBuild is GeographyWithin test = new GeographyWithin(geography, 0.001, coord);

 

Thanks,

 

 

Dr. Tyler J. Grant

Postdoctoral Associate

Department of Natural Resource Ecology and Management

Iowa State University

339 Science II

Ames, IA 50011

 


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest