-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New strategy : Retain up from polygon #310
Merged
leonardehrenfried
merged 5 commits into
OneBusAway:master
from
tisseo-deploy:237-RetainUpFromPolygon-strategy
Dec 12, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f76b247
create new transform operation RemoveStopsOutsidePolygone: remove sto…
333f677
Retain up from polygon, add test file and document strategy
SeifGhz 1cbaef4
Merge branch 'OneBusAway:master' into 237-RetainUpFromPolygon-strategy
tisseo-deploy af9cb26
Change buildPolygon and the check function into the setter
SeifGhz c573cfa
Update documentation for RetainUpFromPolygon strategy with detailed r…
SeifGhz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...s-transformer/src/main/java/org/onebusaway/gtfs_transformer/impl/RetainUpFromPolygon.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.onebusaway.gtfs_transformer.impl; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.locationtech.jts.io.ParseException; | ||
import org.locationtech.jts.io.WKTReader; | ||
import org.locationtech.jts.geom.*; | ||
|
||
import org.onebusaway.csv_entities.schema.annotations.CsvField; | ||
import org.onebusaway.gtfs.model.IdentityBean; | ||
import org.onebusaway.gtfs.model.Stop; | ||
import org.onebusaway.gtfs.serialization.GtfsEntitySchemaFactory; | ||
import org.onebusaway.gtfs.services.GtfsMutableRelationalDao; | ||
import org.onebusaway.gtfs_transformer.factory.EntityRetentionGraph; | ||
import org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy; | ||
import org.onebusaway.gtfs_transformer.services.TransformContext; | ||
|
||
public class RetainUpFromPolygon implements GtfsTransformStrategy { | ||
|
||
@CsvField(optional = false) | ||
private String polygon; | ||
|
||
@CsvField(ignore = true) | ||
private Geometry polygonGeometry; | ||
|
||
public void setPolygon(String polygon) { | ||
this.polygon = polygon; | ||
this.polygonGeometry = buildPolygon(polygon); | ||
|
||
if (this.polygonGeometry == null || !this.polygonGeometry.isValid() || this.polygonGeometry.isEmpty()) { | ||
throw new IllegalArgumentException("The provided polygon is invalid or empty."); | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.getClass().getSimpleName(); | ||
} | ||
|
||
@Override | ||
public void run(TransformContext transformContext, GtfsMutableRelationalDao gtfsMutableRelationalDao) { | ||
EntityRetentionGraph graph = new EntityRetentionGraph(gtfsMutableRelationalDao); | ||
graph.setRetainBlocks(false); | ||
// browse all stops and retain only those inside polygon/multipolygon | ||
for (Stop stop : gtfsMutableRelationalDao.getAllStops()) { | ||
if (insidePolygon(polygonGeometry,stop.getLon(),stop.getLat())){ | ||
graph.retain(stop, true); | ||
} | ||
} | ||
|
||
// remove non retained objects | ||
for (Class<?> entityClass : GtfsEntitySchemaFactory.getEntityClasses()) { | ||
List<Object> objectsToRemove = new ArrayList<Object>(); | ||
for (Object entity : gtfsMutableRelationalDao.getAllEntitiesForType(entityClass)) { | ||
if (!graph.isRetained(entity)){ | ||
objectsToRemove.add(entity); | ||
} | ||
} | ||
for (Object toRemove : objectsToRemove){ | ||
gtfsMutableRelationalDao.removeEntity((IdentityBean<Serializable>) toRemove); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Creates a Geometry object (polygon or multi-polygon) from the provided WKT string. | ||
* | ||
* @param polygonWKT The WKT representation of the polygon. | ||
* @return The Geometry object. | ||
* @throws IllegalArgumentException if the WKT string is invalid or cannot be parsed. | ||
*/ | ||
private Geometry buildPolygon(String polygonWKT) { | ||
WKTReader reader = new WKTReader(); | ||
try{ | ||
return reader.read(polygonWKT); | ||
} catch (ParseException e){ | ||
throw new IllegalArgumentException( | ||
String.format("Error parsing WKT string: %s", e.getMessage()), e | ||
); | ||
} | ||
} | ||
/* | ||
* insidePolygon Checks whether a given point (specified by its longitude and latitude) is inside a given polygon or multipolygon. | ||
* | ||
* @param geometry The Geometry object representing the polygon or multipolygon. | ||
* @param lon the longitude of the point to check. | ||
* @param lat the latitude of the point to check. | ||
* @return true if the point is within the boundaries of the geometry; false otherwise. | ||
*/ | ||
private boolean insidePolygon(Geometry geometry, double lon, double lat) { | ||
GeometryFactory geometryFactory = new GeometryFactory(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wil merge this in and then make this an instance of the class instead. |
||
Point point = geometryFactory.createPoint(new Coordinate(lon, lat)); | ||
return geometry.contains(point); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...ansformer/src/test/java/org/onebusaway/gtfs_transformer/impl/RetainUpFromPolygonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.onebusaway.gtfs_transformer.impl; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.onebusaway.gtfs.services.GtfsMutableRelationalDao; | ||
import org.onebusaway.gtfs.services.MockGtfs; | ||
import org.onebusaway.gtfs_transformer.services.TransformContext; | ||
|
||
public class RetainUpFromPolygonTest { | ||
|
||
private RetainUpFromPolygon retainUpFromPolygon = new RetainUpFromPolygon(); | ||
private TransformContext _context = new TransformContext(); | ||
private MockGtfs _gtfs; | ||
|
||
@BeforeEach | ||
public void setup() throws IOException{ | ||
|
||
_gtfs = MockGtfs.create(); | ||
// Insert mock data into the GTFS for testing: | ||
// 1 agency | ||
_gtfs.putAgencies(1); | ||
// 4 routes | ||
_gtfs.putRoutes(4); | ||
// 4 trips | ||
_gtfs.putTrips(4, "r$0","sid$0"); | ||
// 8 stops | ||
_gtfs.putStops(8); | ||
// 13 stop times | ||
_gtfs.putLines("stop_times.txt", | ||
"trip_id,arrival_time,departure_time,stop_id,stop_sequence,stop_headsign,pickup_type,drop_off_type,shape_dist_traveled", | ||
// Trip t0: sequence of stops s0,s1,s2,s3 | ||
"t0,08:00:00,08:25:00,s0,0,,,,", | ||
"t0,08:30:00,08:55:00,s1,1,,,,", | ||
"t0,09:00:00,09:55:00,s2,2,,,,", | ||
"t0,10:00:00,10:30:00,s3,3,,,,", | ||
// Trip t1: reverse sequence of stops s3,s2,s1,s0 | ||
"t1,08:00:00,08:25:00,s3,0,,,,", | ||
"t1,08:30:00,08:55:00,s2,1,,,,", | ||
"t1,09:00:00,09:55:00,s1,2,,,,", | ||
"t1,10:00:00,10:00:00,s0,3,,,,", | ||
// Trip t2: sequence of stops s3,s4,s5 | ||
"t2,10:00:00,10:55:00,s3,0,,,,", | ||
"t2,11:00:00,11:25:00,s4,1,,,,", | ||
"t2,11:30:00,11:55:00,s5,2,,,,", | ||
// Trip t3: Additional stops | ||
"t3,12:00:00,12:25:00,s6,0,,,,", | ||
"t3,12:30:00,12:55:00,s7,1,,,,"); | ||
} | ||
|
||
@Test | ||
public void testRetainUpFromPolygonTest() throws IOException { | ||
GtfsMutableRelationalDao dao = _gtfs.read(); | ||
|
||
// Define a polygon in WKT (Well-Known Text) format | ||
// This polygon is designed to include only the first 4 stops (S0 to S4) | ||
String polygonWKT = "POLYGON ((-122.308 47.653, -122.308 47.666, -122.307 47.666, -122.307 47.665, -122.307 47.661, -122.307 47.657, -122.307 47.653, -122.308 47.653))"; | ||
retainUpFromPolygon.setPolygon(polygonWKT); | ||
|
||
// Execute the retainUpFromPolygon strategy based on the polygon | ||
retainUpFromPolygon.run(_context, dao); | ||
|
||
// Verify that the number of routes is reduced to 3 | ||
assertEquals(3,dao.getAllRoutes().size()); | ||
|
||
// Verify that the number of trips is reduced to 3 | ||
assertEquals(3,dao.getAllTrips().size()); | ||
|
||
// Verify that the number of stops is reduced to 6 | ||
assertEquals(6,dao.getAllStops().size()); | ||
|
||
// Verify that the number of stop times is reduced to 11 | ||
assertEquals(11,dao.getAllStopTimes().size()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must admit that I find the terminology of "retaining up" and "retaining down" confusing. What do you mean by that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We did'nt found a better term. This one is direct from the existing retain algotithm that seems very stable.
"Up" means that the algorithm will traverse up the entity dependency tree by applying retainUp to each of them. Starting from the stop, retainUp will be applied to the stop_times referencing this stop, then to the trips, and so on.
Then, when the base of the tree is reached, it applies retainDown to all the traversed entities. Therefore, all the trips of the route and then all the stop_times of each trip will be tagged as "retain".
Entities not marked as "retain" will be deleted.
The goal of the strategy is to retain all entities that are directly or indirectly linked to the area.
We will add this brief explanation to the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, that's fine.