[Geotools-gt2-users] Re moving all features in a datastore...

2008-01-11 Thread leaffan

Hejhej,
I've come up with a way to remove all features of a datastore... However,
I'm quite sure that there are more efficient ways to do this, getting rid of
this awkward iteration. Maybe someone can enlighten me.

Thanks very much in advance.

Markus

Code, created for shapefiles...

ShapefileDataStore src...;

try {
String typeName = src.getTypeNames()[0];

DefaultTransaction tran = new DefaultTransaction(RemoveAll);
FeatureStore stor = (FeatureStore)
src.getFeatureSource(typeName);
stor.setTransaction(tran);
FeatureCollection   coll =
src.getFeatureSource(typeName).getFeatures();
FeatureIterator  iter = coll.features();
FilterFactory fact =
CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());

SetFeatureId toRemove = new HashSetFeatureId();

while (iter.hasNext()) {
Feature ft = iter.next();
toRemove.add(fact.featureId(ft.getID()));
}
iter.close();

Filter filter = fact.id(toRemove);

try {
stor.removeFeatures(filter);
tran.commit();
}
catch (Exception ex) {
System.out.println(something wrong...);
tran.rollback();
}

} catch (Exception ex) {
ex.printStackTrace();
}


-- 
View this message in context: 
http://www.nabble.com/Removing-all-features-in-a-datastore...-tp14753911p14753911.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.


-
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
___
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users


Re: [Geotools-gt2-users] Re moving all features in a datastore...

2008-01-11 Thread Andrea Aime
leaffan ha scritto:
 Hejhej,
 I've come up with a way to remove all features of a datastore... However,
 I'm quite sure that there are more efficient ways to do this, getting rid of
 this awkward iteration. Maybe someone can enlighten me.

FeatureStore stor = (FeatureStore) src.getFeatureSource(typeName);
stor.removeFeatures(Filter.INCLUDE);

(that's on gt2 2.4.x, on previous versions that Filter.INCLUDE should
be Filter.NONE).

Cheers
Andrea

-
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
___
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users


Re: [Geotools-gt2-users] Re moving all features in a datastore...

2008-01-11 Thread Andrea Aime
leaffan ha scritto:
 Hejhej,
 I've come up with a way to remove all features of a datastore... However,
 I'm quite sure that there are more efficient ways to do this, getting rid of
 this awkward iteration. Maybe someone can enlighten me.

FeatureStore stor = (FeatureStore) src.getFeatureSource(typeName);
stor.removeFeatures(Filter.INCLUDE);

(that's on gt2 2.4.x, on previous versions that Filter.INCLUDE should
be Filter.NONE).

Cheers
Andrea


 Thanks very much in advance.
 
 Markus
 
 Code, created for shapefiles...
 
 ShapefileDataStore src...;
 
 try {
 String typeName = src.getTypeNames()[0];
 
 DefaultTransaction tran = new DefaultTransaction(RemoveAll);
 FeatureStore stor = (FeatureStore)
 src.getFeatureSource(typeName);
 stor.setTransaction(tran);
 FeatureCollection   coll =
 src.getFeatureSource(typeName).getFeatures();
 FeatureIterator  iter = coll.features();
 FilterFactory fact =
 CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
 
 SetFeatureId toRemove = new HashSetFeatureId();
 
 while (iter.hasNext()) {
 Feature ft = iter.next();
 toRemove.add(fact.featureId(ft.getID()));
 }
 iter.close();
 
 Filter filter = fact.id(toRemove);
 
 try {
 stor.removeFeatures(filter);
 tran.commit();
 }
 catch (Exception ex) {
 System.out.println(something wrong...);
 tran.rollback();
 }
 
 } catch (Exception ex) {
 ex.printStackTrace();
 }
 
 


-
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
___
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users


[Geotools-gt2-users] Splitting LineStrings at their intersection

2008-01-11 Thread Chris
Hi,

After loading a shapefile (ESRI), I created a graph using a
DirectedLineStringGraphGenerator. The creation went fine but when using a
DijkstraShortestPathFinder.getPath() on it, I noticed that there was a
problem because it returned null. Apparently, the linestring graph generator
does not take internal intersections of
lines into account when building the graph. Hence, I decided to code a
routine to split the LineStrings at their intersection as advised there:
http://www.nabble.com/Re%3A-LineString-Graph-Traversal-p1489791.html

The problem is that My routine is taking forever to do the splitting.
Either, I'm doing it wrong or it is not optimized enough. After waiting for
10 minutes, it is still not finished (although, the shapefile is not that
big). Could someone have a look at my function and see what the problem is?

Thanks in advance,

Best regards,
Chris.
private VectorLineString splitLines(VectorLineString lines) {
for (int i = 0; i  lines.size() - 1; ++i) {
LineString l1 = (LineString) lines.get(i);
for (int j = i+1; j  lines.size(); ++j) {
LineString l2 = (LineString) lines.get(j);
Geometry gc = l1.intersection(l2);
if (gc.getNumGeometries()  0) {
// Intersection
Point p = (Point) gc.getGeometryN(0);
if (l2.getStartPoint() != p  l2.getEndPoint() != p) {
// Remove old line from table
lines.remove(j);
// Split into two lines
Coordinate[] c = new 
Coordinate[]{(l2.getStartPoint()).getCoordinate(), p.getCoordinate()};
lines.add(j, new LineString(new 
CoordinateArraySequence(c), new GeometryFactory()));
c = new Coordinate[]{p.getCoordinate(), 
(l2.getEndPoint()).getCoordinate()};
lines.add(j, new LineString(new 
CoordinateArraySequence(c), new GeometryFactory()));
++j;
//System.out.println(Created 1 line);
}
if (l1.getStartPoint() != p  l1.getEndPoint() != p) {
// Remove old line from table
lines.remove(i);
// Split into two lines
Coordinate[] c = new 
Coordinate[]{(l1.getStartPoint()).getCoordinate(), p.getCoordinate()};
LineString new_l = new LineString(new 
CoordinateArraySequence(c), new GeometryFactory()); 
lines.add(i, new_l);
c = new Coordinate[]{p.getCoordinate(), 
(l1.getEndPoint()).getCoordinate()};
lines.add(i+1, new LineString(new 
CoordinateArraySequence(c), new GeometryFactory()));
++j;
//System.out.println(Created 1 line);
l1 = (LineString) new_l;
}
}
}
}
return lines;
}
-
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace___
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users


Re: [Geotools-gt2-users] Splitting LineStrings at their intersection

2008-01-11 Thread Justin Deoliveira
Hi Chris,

I would have to mock up some data and test it but I dont think the
problem is the method but in the management of the data structure. The
code is modifying the same list it is iterating over... which is
generally not a great idea.

I recommend restructuring the algorithm so that when two new lines are
created they are added to a separate list. When the outer loop termines
add the new lines to the original list and repeat the algorithm
terminating when there are no more new lines.

Hope that helps. I could be wrong as i said since i have not run the
code. If you find you continue to have the same problem let us know .

-Justin

Chris wrote:
 Hi,
 
 After loading a shapefile (ESRI), I created a graph using a
 DirectedLineStringGraphGenerator. The creation went fine but when using
 a DijkstraShortestPathFinder.getPath() on it, I noticed that there was a
 problem because it returned null. Apparently, the linestring graph
 generator does not take internal intersections of
 lines into account when building the graph. Hence, I decided to code a
 routine to split the LineStrings at their intersection as advised there:
 http://www.nabble.com/Re%3A-LineString-Graph-Traversal-p1489791.html
 
 The problem is that My routine is taking forever to do the splitting.
 Either, I'm doing it wrong or it is not optimized enough. After waiting
 for 10 minutes, it is still not finished (although, the shapefile is not
 that big). Could someone have a look at my function and see what the
 problem is?
 
 Thanks in advance,
 
 Best regards,
 Chris.
 !DSPAM:4007,4787a0db264908992556831!
 
 
 
 
 -
 Check out the new SourceForge.net Marketplace.
 It's the best place to buy or sell services for
 just about anything Open Source.
 http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
 
 !DSPAM:4007,4787a0db264908992556831!
 
 
 
 
 ___
 Geotools-gt2-users mailing list
 Geotools-gt2-users@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
 
 
 !DSPAM:4007,4787a0db264908992556831!


-- 
Justin Deoliveira
The Open Planning Project
http://topp.openplans.org

-
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
___
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users


Re: [Geotools-gt2-users] Splitting LineStrings at their intersection

2008-01-11 Thread Chris
I added some debug, and apparently, the LineString intersection is not due
to my algorithm. It happens approximately 12 times in the first loop
iteration, then it does not happen anymore. This was probably due to my
shapefile.

So I chose to ignore the LineString intersections and now my algorithm has
been running for approximately half an hour and it is the fourth iteration
of the while loop (still unfinished). I added some debug to see how many
LineStrings were created for each iteration:
Added 2200 new lines
Added 7666 new lines
Added 26234 new lines

Is my algorithm ok? Is it normal it is taking so long? Is there any way I
can optimize this? If not, I guess I need to save the result somewhere in a
file and work from the result from now on because it is taking too much
time.

Regards,
Chris.

On Jan 11, 2008 9:28 PM, Chris [EMAIL PROTECTED] wrote:

 I tried to use another Vector as you advised. This way, the size the the
 lines Vector is not changing while iterating on it. I'm joining the new code
 to this mail.

 It is taking quite some time (2-3 minutes) but this time I got an
 exception:
 java.lang.ClassCastException: com.vividsolutions.jts.geom.LineStringcannot be 
 cast to
 com.vividsolutions.jts.geom.Point

 at this line:
 Point p = (Point) gc.getGeometryN(0);

 where gc is the intersection between the two lines. Apparently, the
 intersection between two LineStrings can be a LineString. I guess this means
 that the two lines are identical? How could this happen? Is my algorithm
 still wrong? or maybe I should just ignore when this case happens?

 Regards,
 Chris.


 On Jan 11, 2008 7:28 PM, Justin Deoliveira [EMAIL PROTECTED] wrote:

  Hi Chris,
 
  I would have to mock up some data and test it but I dont think the
  problem is the method but in the management of the data structure. The
  code is modifying the same list it is iterating over... which is
  generally not a great idea.
 
  I recommend restructuring the algorithm so that when two new lines are
  created they are added to a separate list. When the outer loop termines
  add the new lines to the original list and repeat the algorithm
  terminating when there are no more new lines.
 
  Hope that helps. I could be wrong as i said since i have not run the
  code. If you find you continue to have the same problem let us know .
 
  -Justin
 
  Chris wrote:
   Hi,
  
   After loading a shapefile (ESRI), I created a graph using a
   DirectedLineStringGraphGenerator. The creation went fine but when
  using
   a DijkstraShortestPathFinder.getPath() on it, I noticed that there was
  a
   problem because it returned null. Apparently, the linestring graph
   generator does not take internal intersections of
   lines into account when building the graph. Hence, I decided to code a
 
   routine to split the LineStrings at their intersection as advised
  there:
   http://www.nabble.com/Re%3A-LineString-Graph-Traversal-p1489791.html
  
   The problem is that My routine is taking forever to do the
  splitting.
   Either, I'm doing it wrong or it is not optimized enough. After
  waiting
   for 10 minutes, it is still not finished (although, the shapefile is
  not
   that big). Could someone have a look at my function and see what the
   problem is?
  
   Thanks in advance,
  
   Best regards,
   Chris.
   !DSPAM:4007,4787a0db264908992556831!
  
  
  
  
  
  
  -
   Check out the new SourceForge.net Marketplace.
   It's the best place to buy or sell services for
   just about anything Open Source.
  
  http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
  
   !DSPAM:4007,4787a0db264908992556831!
  
  
  
  
  
   ___
   Geotools-gt2-users mailing list
   Geotools-gt2-users@lists.sourceforge.net
   https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
  
  
   !DSPAM:4007,4787a0db264908992556831!
 
 
  --
  Justin Deoliveira
  The Open Planning Project
  http://topp.openplans.org
 


-
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace___
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users


Re: [Geotools-gt2-users] Splitting LineStrings at their intersection

2008-01-11 Thread Chris
I tried to use another Vector as you advised. This way, the size the the
lines Vector is not changing while iterating on it. I'm joining the new code
to this mail.

It is taking quite some time (2-3 minutes) but this time I got an exception:
java.lang.ClassCastException: com.vividsolutions.jts.geom.LineString cannot
be cast to com.vividsolutions.jts.geom.Point

at this line:
Point p = (Point) gc.getGeometryN(0);

where gc is the intersection between the two lines. Apparently, the
intersection between two LineStrings can be a LineString. I guess this means
that the two lines are identical? How could this happen? Is my algorithm
still wrong? or maybe I should just ignore when this case happens?

Regards,
Chris.

On Jan 11, 2008 7:28 PM, Justin Deoliveira [EMAIL PROTECTED] wrote:

 Hi Chris,

 I would have to mock up some data and test it but I dont think the
 problem is the method but in the management of the data structure. The
 code is modifying the same list it is iterating over... which is
 generally not a great idea.

 I recommend restructuring the algorithm so that when two new lines are
 created they are added to a separate list. When the outer loop termines
 add the new lines to the original list and repeat the algorithm
 terminating when there are no more new lines.

 Hope that helps. I could be wrong as i said since i have not run the
 code. If you find you continue to have the same problem let us know .

 -Justin

 Chris wrote:
  Hi,
 
  After loading a shapefile (ESRI), I created a graph using a
  DirectedLineStringGraphGenerator. The creation went fine but when using
  a DijkstraShortestPathFinder.getPath() on it, I noticed that there was a
  problem because it returned null. Apparently, the linestring graph
  generator does not take internal intersections of
  lines into account when building the graph. Hence, I decided to code a
  routine to split the LineStrings at their intersection as advised there:
  http://www.nabble.com/Re%3A-LineString-Graph-Traversal-p1489791.html
 
  The problem is that My routine is taking forever to do the splitting.
  Either, I'm doing it wrong or it is not optimized enough. After waiting
  for 10 minutes, it is still not finished (although, the shapefile is not
  that big). Could someone have a look at my function and see what the
  problem is?
 
  Thanks in advance,
 
  Best regards,
  Chris.
  !DSPAM:4007,4787a0db264908992556831!
 
 
  
 
 
 -
  Check out the new SourceForge.net Marketplace.
  It's the best place to buy or sell services for
  just about anything Open Source.
 
 http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
 
  !DSPAM:4007,4787a0db264908992556831!
 
 
  
 
  ___
  Geotools-gt2-users mailing list
  Geotools-gt2-users@lists.sourceforge.net
  https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
 
 
  !DSPAM:4007,4787a0db264908992556831!


 --
 Justin Deoliveira
 The Open Planning Project
 http://topp.openplans.org

private VectorLineString splitLines(VectorLineString lines) {
VectorLineString new_lines = new VectorLineString();
boolean first = true;
int imax = lines.size()-1;
do {
if(!first) {
imax = new_lines.size()-1; // Optimization
// Merge the two vectors
lines.addAll(0, new_lines);
new_lines.clear();
}
for (int i = 0; i  imax; ++i) {
LineString l1 = (LineString) lines.get(i);
int jmax = lines.size();
for (int j = i + 1; j  jmax; ++j) {
LineString l2 = (LineString) lines.get(j);
Geometry gc = l1.intersection(l2);
if (gc.getNumGeometries()  0) {
// Intersection
Point p = (Point) gc.getGeometryN(0);
if (l2.getStartPoint() != p  l2.getEndPoint() != p) {
// Remove old line from table
lines.remove(j);
// Split into two lines
Coordinate[] c = new 
Coordinate[]{(l2.getStartPoint()).getCoordinate(), p.getCoordinate()};
lines.add(j, new LineString(new 
CoordinateArraySequence(c), new GeometryFactory()));
c = new Coordinate[]{p.getCoordinate(), 
(l2.getEndPoint()).getCoordinate()};
new_lines.add(new LineString(new 
CoordinateArraySequence(c), new GeometryFactory()));
}
if (l1.getStartPoint() != p  l1.getEndPoint() != p) {
// Remove old line from table

Re: [Geotools-gt2-users] Splitting LineStrings at their intersection

2008-01-11 Thread Justin Deoliveira
Chris wrote:
 I added some debug, and apparently, the LineString intersection is not
 due to my algorithm. It happens approximately 12 times in the first loop
 iteration, then it does not happen anymore. This was probably due to my
 shapefile.
 
 So I chose to ignore the LineString intersections and now my algorithm
 has been running for approximately half an hour and it is the fourth
 iteration of the while loop (still unfinished). I added some debug to
 see how many LineStrings were created for each iteration:
 Added 2200 new lines
 Added 7666 new lines
 Added 26234 new lines
 
 Is my algorithm ok? Is it normal it is taking so long? Is there any way
 I can optimize this? If not, I guess I need to save the result somewhere
 in a file and work from the result from now on because it is taking too
 much time.

Intersection is quite an expensive operation. I am not surprised it is
taking this long with any non-trivial amount of data. Luckily there are
some easy ways to optimize this. The best way to be to use a spatial
index instead of looping through every line string and doing an
intersection. So the the first step of your algorithm will be to
populate the index with all of your lines. Its pretty easy to use:

SpatialIndex index = ...;
for ( LineString l : lines ) {
  index.insert( l.getEnvelopInternal(), l );
}

Then you can replace the second loop with a lookup in the index.

//current line being processed
LineString l = ...;

//do a looup in the index
List close = index.query( l.getEnvelopeInternal() );

//do intersection on close lines
for ( LineString c : close ) {
   l.intersect( c );
}

.. etc..


This will greatly reduce the number of intersections you have to do and
you should see a pretty drastic performance improvement. There are two
implementations of com.vividsolutions.jts.index.SpatialIndex: STRTree
and QuadTree. The STRTree will give you best performance but it is
static, which means you cant update over the stage of your algorithm
which you will probably want to do with the new lines that are produced.
So you will probably have to just use a the QuadTree implementation.
Either one should suffice in this case.

Try that and let me know how it works.

-Justin

 
 Regards,
 Chris.
 
 On Jan 11, 2008 9:28 PM, Chris [EMAIL PROTECTED]
 mailto:[EMAIL PROTECTED] wrote:
 
 I tried to use another Vector as you advised. This way, the size the
 the lines Vector is not changing while iterating on it. I'm joining
 the new code to this mail.
 
 It is taking quite some time (2-3 minutes) but this time I got an
 exception:
 java.lang.ClassCastException: com.vividsolutions.jts.geom.LineString
 cannot be cast to com.vividsolutions.jts.geom.Point
 
 at this line:
 Point p = (Point) gc.getGeometryN(0);
 
 where gc is the intersection between the two lines. Apparently, the
 intersection between two LineStrings can be a LineString. I guess
 this means that the two lines are identical? How could this happen?
 Is my algorithm still wrong? or maybe I should just ignore when this
 case happens?
 
 Regards,
 Chris.
 
 
 On Jan 11, 2008 7:28 PM, Justin Deoliveira  [EMAIL PROTECTED]
 mailto:[EMAIL PROTECTED] wrote:
 
 Hi Chris,
 
 I would have to mock up some data and test it but I dont think the
 problem is the method but in the management of the data
 structure. The
 code is modifying the same list it is iterating over... which is
 generally not a great idea.
 
 I recommend restructuring the algorithm so that when two new
 lines are
 created they are added to a separate list. When the outer loop
 termines
 add the new lines to the original list and repeat the algorithm
 terminating when there are no more new lines.
 
 Hope that helps. I could be wrong as i said since i have not run the
 code. If you find you continue to have the same problem let us
 know .
 
 -Justin
 
 Chris wrote:
  Hi,
 
  After loading a shapefile (ESRI), I created a graph using a
  DirectedLineStringGraphGenerator. The creation went fine but
 when using
  a DijkstraShortestPathFinder.getPath() on it, I noticed that
 there was a
  problem because it returned null. Apparently, the linestring graph
  generator does not take internal intersections of
  lines into account when building the graph. Hence, I decided
 to code a
  routine to split the LineStrings at their intersection as
 advised there:
 
 http://www.nabble.com/Re%3A-LineString-Graph-Traversal-p1489791.html
 http://www.nabble.com/Re%3A-LineString-Graph-Traversal-p1489791.html
 
  The problem is that My routine is taking forever to do the
 splitting.
  Either, I'm doing it wrong or it is not optimized enough.
 After