Hi,
> Thanks a lot for the improvements.  However, I would like to ask you to help 
> with one more fix. Now the script fires the requests automatically and it is 
> dangerous if user has selected a layer with thousands of features. Could you 
> show how to limit the maximum number of requests to 10? Generally it is good 
> to be able to do several routings at a time because it gives a simple way for 
> the user to use interim fixed route points by digitizing the route as 
> connected lines but I think that 10 legs should be enough.
I see,
In your initial script, there was a counter.
I removed it, but you can add it again and use it this way.
count = 0;
for (Iterator it = ....) {

     if (count++ > 10) break;
}

Also an improvement I was too lazy to do is to put result in
a new layer...

Michaël

  -Jukka Rahkonen- Michaël Michaud wrote:
>> Hi Jukka,
>>
>> Good job,
>>
>> With a few more lines, you can get the path in your map (hereafter, kml
>> parsing is very crude, but it shows how powerful beanshell scripting can be).
>>
>> {
>> import com.vividsolutions.jts.geom.*;
>> import com.vividsolutions.jts.io.WKTReader;
>> import com.vividsolutions.jump.feature.*;
>> import com.vividsolutions.jump.geom.EnvelopeUtil;
>> import com.vividsolutions.jump.workbench.model.*;
>> import java.io.*;
>> import java.net.URL;
>>
>>       fc =
>> wc.getLayerNamePanel().getSelectedLayers()[0].getFeatureCollectionWrap
>> per();
>>       paths = new ArrayList();;
>>       for (Iterator i = fc.getFeatures().iterator(); i.hasNext();) {
>>           Feature feature = (Feature) i.next();
>>           LineString lineString = (LineString)feature.getGeometry();
>>           URL url;
>>           try {
>>               // get URL content
>>               url = new
>> URL("http://www.yournavigation.org/api/1.0/gosmore.php?format=kml&fla
>> t="
>>                   +lineString.getStartPoint().getY()+"&flon="
>>                   +lineString.getStartPoint().getX()+"&tlat="
>>                   +lineString.getEndPoint().getY()+"&tlon="
>>                   +lineString.getEndPoint().getX()
>>                   +"&v=motorcar"
>>                   +"&fast=1"
>>                   +"&layer=mapnik");
>>               print(url);
>>               URLConnection conn = url.openConnection();
>>
>>               // open the stream and put it into BufferedReader
>>               BufferedReader br = new BufferedReader(
>>                                  new
>> InputStreamReader(conn.getInputStream()));
>>               String inputLine;
>>               StringBuffer sb = new StringBuffer();
>>               while ((inputLine = br.readLine()) != null) {
>>                   sb.append(inputLine).append("\n");
>>               }
>>               wkt = sb.toString()
>>                       .replaceAll("(?s).*<coordinates>\\s*", "LINESTRING(")
>>                       .replaceAll("(?s)\\s*</coordinates>.*", ")")
>>                       .replaceAll("(?s)\\s+", "@")
>>                       .replaceAll("(?s),", " ")
>>                       .replaceAll("(?s)@", ",");
>>               print(wkt);
>>               bf = new BasicFeature(fc.getFeatureSchema());
>>               bf.setGeometry(new WKTReader().read(wkt));
>>               paths.add(bf);
>>
>>               br.close();
>>
>>           } catch (MalformedURLException e) {
>>               e.printStackTrace();
>>           } catch (IOException e) {
>>               e.printStackTrace();
>>           }
>>       }
>>       fc.addAll(paths);
>> }
>>
>>
>>
>> Michaël
>>
>>
>>> Hi,
>>>
>>> Here is a demo script which finds route from the service or
>> yournavigation.org. Save the following code into lib\ext\BeanTools, draw
>> one or more lines with OpenJUMP by using EPSG:4326 system, run the tool
>> and copy the URLs from the result window into browser and you will get the
>> routes in KML format. Script creates a request which follows the YOURS
>> syntax http://wiki.openstreetmap.org/wiki/YOURS and it is simple to modify
>> to use other YOURS parameters. I also made another version which is getting
>> routes from the service of the Helsinki Region Traffic and it works fine too.
>>> With a little bit of work it should be possible to make an OpenJUMP routing
>> service plugin. Creating a request is easy but a very good plugin should be
>> able to handle coordinate conversions between OJ project coordinate
>> system and the one used by the routing service. And then OpenJUMP should
>> be able to parse the returning route which is typically in GPX or KML format.
>> However, I tested also a workaround by using GDAL in between. OpenJUMP
>> can create a ogr2ogr request and ogr2ogr can save the result as a shapefile
>> and possibly converted into another projection.
>>>
>>>
>>> Get_YOURS_Route.bsh
>>> ==============
>>> {
>>> import com.vividsolutions.jts.geom.*;
>>> import com.vividsolutions.jump.feature.*;
>>> import com.vividsolutions.jump.geom.EnvelopeUtil;
>>> import com.vividsolutions.jump.workbench.model.*;
>>>
>>>           htmlFrame = wc.workbench.frame.outputFrame;
>>>           htmlFrame.createNewDocument();
>>>           htmlFrame.setTitle("Output for gdal_translate");
>>>           fc =
>> wc.getLayerNamePanel().getSelectedLayers()[0].getFeatureCollectionWrap
>> per();
>>>           int j=1;
>>>           for (Iterator i = fc.getFeatures().iterator(); i.hasNext();) {
>>>                   Feature feature = (Feature) i.next();
>>>                   LineString lineString = (LineString)feature.getGeometry();
>>>
>> htmlFrame.addText("http://www.yournavigation.org/api/1.0/gosmore.php?
>> format=kml&flat="
>>>                           +lineString.getStartPoint().getY()+"&flon="
>>>                           +lineString.getStartPoint().getX()+"&tlat="
>>>                           +lineString.getEndPoint().getY()+"&tlon="
>>>                           +lineString.getEndPoint().getX()
>>>                           +"&v=motorcar"
>>>                           +"&fast=1"
>>>                           +"&layer=mapnik");
>>>                   j++;
>>>           }
>>>           wc.workbench.frame.flash(htmlFrame);
>>>           htmlFrame.surface();
>>> }
>>>
>>>
>>> With small modifications the script works with the routing service of
>>> the Helsinki Region Traffic. Coordinates must be in EPSG:2392 system.
>>> Can be tested for example with LINESTRING (2538008.7818696885
>>> 6679830.130311615, 2538096.0339943343 6679899.818696884)
>>>
>>>
>>>
>>>
>> htmlFrame.addText("\"http://pk.hsl.fi/getroute/?format=gpx&profile=klero
>> weighted&from=location*Koordinaatti["
>>>                           +lineString.getStartPoint().getX()+", "
>>>                           +lineString.getStartPoint().getY()+"]"
>>>                           +"*"+lineString.getStartPoint().getX()
>>>                           +"*"+lineString.getStartPoint().getY()
>>>                           +"&to=location*Koordinaatti["
>>>                           +lineString.getEndPoint().getX()+", "
>>>                           +lineString.getEndPoint().getY()+"]"
>>>                           +"*"+lineString.getEndPoint().getX()
>>>                           +"*"+lineString.getEndPoint().getY()
>>>                           +"\""  );
>>>
>>>
>>>
>>> -Jukka-
>>>
>>> ________________________________________
>>> Michaël Michaud wrote:
>>>
>>>
>>>> Hi Jukka,
>>>> You can easily pick start and end point with Geometry geometry =
>>>> feature.getGeometry();
>>> // this will return something whatever the geometry is Coordinate[] cc
>>> = geometry.getCoordinates(); x_start = cc[0].x; y_start = cc[0].y;
>>> x_end = cc[cc.length-1].x; y_end = cc[cc.length-1].y;
>>>
>>> // this is more readable if you make sure your geometry is a
>>> linestring before LineString lineString =
>>> (LineString)feature.getGeometry();
>>> x_start = lineString.getStartPoint().getX();
>>> y_start = lineString.getStartPoint().getY();
>>> x_end = lineString.getEndPoint().getX(); y_end =
>>> lineString.getEndPoint().getX();
>>>
>>>> Warning : I did not check the code
>>>> Michaël
>>>> PS let us know what you achieved with this code
>>>>> Hi,
>>>>>
>>>>> How could I pick Startpoint_X, Startpoint_Y, Endpoint_X,Endpoint_Y
>> from a linestring with BeanShell? Meaning is to build a http request for an
>> external routing service. Something similar is done in the following code 
>> with
>> envelope of a geometry for building a WFS GetFeature request with BBOX
>> filter.
>>>>> Geometry geometry = feature.getGeometry();
>>>>>                    Envelope env = geometry.getEnvelopeInternal();
>>>>>                    htmlFrame.addText("wget "
>>>>>                            +"\"http://hip.latuviitta.org/cgi-bin/";
>>>>>                            +"tinyows?"
>>>>>                            +"SERVICE=WFS"
>>>>>                            +"&VERSION=1.0.0"
>>>>>                            +"&REQUEST=GetFeature"
>>>>>                            +"&TYPENAME=lv:municipalities"
>>>>>                            +"&BBOX="
>>>>>                            +env.getMinX()+","
>>>>>                            +env.getMinY()+","
>>>>>                            +env.getMaxX()+","
>>>>>                            +env.getMaxY()+"\""
>>>>>                            +" -O prexif_"
>>>>>                            +j
>>>>>                            +".gml"  );
>>>>>
>>>>> -Jukka Rahkonen-
>>>> ---------------------------------------------------------------------
>>>> --------- Own the Future-Intel&reg; Level Up Game Demo Contest 2013
>>>> Rise to greatness in Intel's independent game demo contest.
>>>> Compete for recognition, cash, and the chance to get your game on
>>>> Steam. $5K grand prize plus 10 genre and skill prizes.
>>>> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
>>>> _______________________________________________
>>>> Jump-pilot-devel mailing list
>>>> Jump-pilot-devel@lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
>>>>
>>>>
>>> ----------------------------------------------------------------------
>>> -------- Own the Future-Intel&reg; Level Up Game Demo Contest 2013
>>> Rise to greatness in Intel's independent game demo contest.
>>> Compete for recognition, cash, and the chance to get your game on
>>> Steam. $5K grand prize plus 10 genre and skill prizes.
>>> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
>>> _______________________________________________
>>> Jump-pilot-devel mailing list
>>> Jump-pilot-devel@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
>>>
>>> ----------------------------------------------------------------------
>>> -------- Own the Future-Intel&reg; Level Up Game Demo Contest 2013
>>> Rise to greatness in Intel's independent game demo contest.
>>> Compete for recognition, cash, and the chance to get your game on
>>> Steam. $5K grand prize plus 10 genre and skill prizes.
>>> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
>>> _______________________________________________
>>> Jump-pilot-devel mailing list
>>> Jump-pilot-devel@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
>>>
>>>
>>
>> ------------------------------------------------------------------------------
>> Own the Future-Intel&reg; Level Up Game Demo Contest 2013 Rise to
>> greatness in Intel's independent game demo contest.
>> Compete for recognition, cash, and the chance to get your game on Steam.
>> $5K grand prize plus 10 genre and skill prizes.
>> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
>> _______________________________________________
>> Jump-pilot-devel mailing list
>> Jump-pilot-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
> ------------------------------------------------------------------------------
> Own the Future-Intel(R) Level Up Game Demo Contest 2013
> Rise to greatness in Intel's independent game demo contest. Compete
> for recognition, cash, and the chance to get your game on Steam.
> $5K grand prize plus 10 genre and skill prizes. Submit your demo
> by 6/6/13. http://altfarm.mediaplex.com/ad/ck/12124-176961-30367-2
> _______________________________________________
> Jump-pilot-devel mailing list
> Jump-pilot-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
>
>


------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire 
the most talented Cisco Certified professionals. Visit the 
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to