Skip to content

Commit ccf435e

Browse files
committed
feat(gis-export): add gis export for stops/routes
1 parent d163d4b commit ccf435e

File tree

4 files changed

+94
-216
lines changed

4 files changed

+94
-216
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.conveyal.datatools.editor.controllers.api;
2+
3+
import com.conveyal.datatools.editor.jobs.GisExport;
4+
import com.conveyal.datatools.manager.models.JsonViews;
5+
import com.conveyal.datatools.manager.utils.json.JsonManager;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import spark.Request;
9+
import spark.Response;
10+
11+
import java.io.BufferedInputStream;
12+
import java.io.BufferedOutputStream;
13+
import java.io.File;
14+
import java.io.FileInputStream;
15+
import java.io.IOException;
16+
import java.nio.file.Files;
17+
import java.nio.file.Path;
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import static spark.Spark.get;
22+
import static spark.Spark.halt;
23+
24+
/**
25+
* Created by landon on 5/30/17.
26+
*/
27+
public class GisController {
28+
public static final JsonManager<GisController> json =
29+
new JsonManager<>(GisController.class, JsonViews.UserInterface.class);
30+
private static final Logger LOG = LoggerFactory.getLogger(GisController.class);
31+
32+
33+
public static FileInputStream exportGis (Request req, Response res) throws IOException {
34+
String type = req.queryParams("type");
35+
List<String> feedIds = Arrays.asList(req.queryParams("feedId").split(","));
36+
File temp = File.createTempFile("gis_" + type, ".zip");
37+
38+
GisExport gisExport = new GisExport(GisExport.Type.valueOf(type), temp, feedIds);
39+
gisExport.run();
40+
41+
FileInputStream fis = new FileInputStream(temp);
42+
43+
res.type("application/zip");
44+
res.header("Content-Disposition", "attachment;filename=" + temp.getName().replaceAll("[^a-zA-Z0-9]", "") + ".zip");
45+
46+
// will not actually be deleted until download has completed
47+
// http://stackoverflow.com/questions/24372279
48+
// temp.delete();
49+
50+
return fis;
51+
}
52+
53+
// res.raw().setContentType("application/zip");
54+
// res.raw().setHeader("Content-Disposition", "attachment; filename=" + temp.getName() + ".zip");
55+
//
56+
// try {
57+
// BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(res.raw().getOutputStream());
58+
// BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(temp));
59+
//
60+
// byte[] buffer = new byte[1024];
61+
// int len;
62+
// while ((len = bufferedInputStream.read(buffer)) > 0) {
63+
// bufferedOutputStream.write(buffer, 0, len);
64+
// }
65+
//
66+
// bufferedOutputStream.flush();
67+
// bufferedOutputStream.close();
68+
// } catch (Exception e) {
69+
// halt(500, "Error serving GTFS+ file");
70+
// }
71+
//
72+
// return res.raw();
73+
74+
public static void register (String apiPrefix) {
75+
get(apiPrefix + "secure/export/gis", GisController::exportGis, json::write);
76+
}
77+
}

src/main/java/com/conveyal/datatools/editor/jobs/GisExport.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.conveyal.datatools.editor.jobs;
22

33
import com.conveyal.datatools.editor.datastore.FeedTx;
4-
import com.conveyal.datatools.manager.models.FeedSource;
54
import com.google.common.io.Files;
65
import com.vividsolutions.jts.geom.Coordinate;
76
import com.vividsolutions.jts.geom.GeometryFactory;
@@ -36,12 +35,12 @@
3635
public class GisExport implements Runnable {
3736
File file;
3837
Type type;
39-
Collection<String> agencyIds;
38+
Collection<String> feedIds;
4039

41-
public GisExport(Type type, File file, Collection<String> agencyIds) {
40+
public GisExport(Type type, File file, Collection<String> feedIds) {
4241
this.type = type;
4342
this.file = file;
44-
this.agencyIds = agencyIds;
43+
this.feedIds = feedIds;
4544
}
4645

4746
@Override
@@ -50,7 +49,7 @@ public void run() {
5049
File outShp = new File(outDir, file.getName().replaceAll("\\.zip", "") + ".shp");
5150

5251
GlobalTx gtx = VersionedDataStore.getGlobalTx();
53-
FeedTx atx = null;
52+
FeedTx feedtx = null;
5453
try {
5554
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
5655

@@ -98,11 +97,11 @@ public void run() {
9897
datastore.createSchema(STOP_TYPE);
9998
featureBuilder = new SimpleFeatureBuilder(STOP_TYPE);
10099

101-
for (String feedId : agencyIds) {
100+
for (String feedId : feedIds) {
102101
EditorFeed fs = gtx.feeds.get(feedId);
103102

104-
atx = VersionedDataStore.getFeedTx(feedId);
105-
for (Stop s : atx.stops.values()) {
103+
feedtx = VersionedDataStore.getFeedTx(feedId);
104+
for (Stop s : feedtx.stops.values()) {
106105
featureBuilder.add(s.location);
107106
featureBuilder.add(s.stopName);
108107
featureBuilder.add(s.stopCode);
@@ -113,7 +112,7 @@ public void run() {
113112
features.add(feature);
114113
}
115114

116-
atx.rollback();
115+
feedtx.rollback();
117116
}
118117
} else if (type.equals(Type.ROUTES)) {
119118
collectionType = ROUTE_TYPE;
@@ -122,14 +121,14 @@ public void run() {
122121

123122
GeometryFactory gf = new GeometryFactory();
124123

125-
for (String feedId : agencyIds) {
124+
for (String feedId : feedIds) {
126125
EditorFeed fs = gtx.feeds.get(feedId);
127126

128-
atx = VersionedDataStore.getFeedTx(feedId);
127+
feedtx = VersionedDataStore.getFeedTx(feedId);
129128

130129
// we loop over trip patterns. Note that this will yield several lines for routes that have
131130
// multiple patterns. There's no real good way to reconcile the shapes of multiple patterns.
132-
for (TripPattern tp : atx.tripPatterns.values()) {
131+
for (TripPattern tp : feedtx.tripPatterns.values()) {
133132
LineString shape;
134133
if (tp.shape != null) {
135134
shape = tp.shape;
@@ -138,13 +137,13 @@ public void run() {
138137
Coordinate[] coords = new Coordinate[tp.patternStops.size()];
139138

140139
for (int i = 0; i < coords.length; i++) {
141-
coords[i] = atx.stops.get(tp.patternStops.get(i).stopId).location.getCoordinate();
140+
coords[i] = feedtx.stops.get(tp.patternStops.get(i).stopId).location.getCoordinate();
142141
}
143142

144143
shape = gf.createLineString(coords);
145144
}
146145

147-
Route r = atx.routes.get(tp.routeId);
146+
Route r = feedtx.routes.get(tp.routeId);
148147

149148
featureBuilder.add(shape);
150149
featureBuilder.add(tp.name);
@@ -165,7 +164,7 @@ public void run() {
165164
features.add(feature);
166165
}
167166

168-
atx.rollback();
167+
feedtx.rollback();
169168
}
170169
}
171170
else
@@ -208,9 +207,9 @@ public void run() {
208207
e.printStackTrace();
209208
} finally {
210209
if (gtx != null) gtx.rollback();
211-
if (atx != null) atx.rollbackIfOpen();
210+
if (feedtx != null) feedtx.rollbackIfOpen();
212211
}
213212
}
214213

215-
public static enum Type { ROUTES, STOPS };
214+
public enum Type { ROUTES, STOPS }
216215
}

src/main/java/com/conveyal/datatools/editor/jobs/ProcessGisExport.java

Lines changed: 0 additions & 199 deletions
This file was deleted.

src/main/java/com/conveyal/datatools/manager/DataManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ private static void registerRoutes() throws IOException {
143143
SnapshotController.register(EDITOR_API_PREFIX);
144144
FeedInfoController.register(EDITOR_API_PREFIX);
145145
FareController.register(EDITOR_API_PREFIX);
146+
GisController.register(EDITOR_API_PREFIX);
146147
}
147148

148149
// log all exceptions to system.out

0 commit comments

Comments
 (0)