Преглед изворни кода

Muudetud gtfs-i importimist.

Ardo Kubjas пре 4 година
родитељ
комит
7cc567ebd4
1 измењених фајлова са 40 додато и 2 уклоњено
  1. 40 2
      R/gtfs.R

+ 40 - 2
R/gtfs.R

@@ -134,7 +134,39 @@ gtfs <- function(conf = NULL) {
           str <- sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' , 'CLOSE_PATH' : 0 , DATE_FORMAT : '', GROUP_FIELD : 'shape_id', ORDER_FIELD : 'shape_pt_sequence', OUTPUT_TEXT_DIR : '/tmp/' }", input, output)
           cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
           system(cmd)
-        } else {
+        } else if (tbl_names[i] == "stops") {
+          stops <- utils::read.csv(file = sprintf("%s/%s.csv", tmp_dir, tbl_names[i]))
+          ## --------------- Points to SpatialPoints -----------------
+          cat("\n----------------\nPeatuste geomeetria loomine\n")
+
+          sl <- stops %>%
+            # dplyr::arrange(.data$shape_id, .data$shape_pt_sequence) %>%
+            sf::st_as_sf(coords = c("lest_y", "lest_x"), agr = "constant") %>%
+            # dplyr::group_by(shape_id) %>%
+            # dplyr::summarise() %>%
+            sf::st_cast("POINT")
+          sf::st_crs(sl) <- 3301
+          # # Test plot
+          # sl %>%
+          #   ggplot2::ggplot() +
+          #   ggplot2::geom_sf()
+          # plot(sl)
+          # Write to database
+          tabeli_suffix <- ""
+          sf::st_write(
+            obj = sl, dsn = conn, layer_options = c("OVERWRITE=yes"),
+            layer = sprintf("%s%s", conf$table, tabeli_suffix)
+          )
+          # Change schema
+          q <- sprintf("
+          drop table if exists %s.%s%s; \
+          ALTER TABLE %s%s SET SCHEMA %s", conf$schema, conf$table, tabeli_suffix, conf$table, tabeli_suffix, conf$schema)
+          cat("\n----------------\nMuudame schema.\n")
+          cat(q)
+          cat("\n\n")
+          DBI::dbExecute(conn, q)
+        }
+        else {
           cmd <- sprintf(
             paste0(
               "ogr2ogr -f PostgreSQL ",
@@ -149,6 +181,11 @@ gtfs <- function(conf = NULL) {
 
       # Lisame andmebaasi seosed
       q <- sprintf("
+      -- Kuna osad shape'id puuduvad lisame \u00FChe t\u00FChja \
+      INSERT INTO %s.shapes (shape_id, \"begin\", \"end\") VALUES( 0, 1, 1); \
+      UPDATE %s.trips SET shape_id='0' WHERE shape_id=''; \
+      ALTER TABLE %s.stop_times ALTER COLUMN stop_id TYPE int4 USING stop_id::int4; \
+      ALTER TABLE %s.trips ALTER COLUMN shape_id TYPE int4 USING shape_id::int4; \
       ALTER TABLE %s.agency ADD CONSTRAINT agency_un UNIQUE (agency_id); \
       ALTER TABLE %s.calendar ADD CONSTRAINT calendar_un UNIQUE (service_id); \
       ALTER TABLE %s.fare_attributes ADD CONSTRAINT fare_attributes_un UNIQUE (fare_id); \
@@ -163,8 +200,9 @@ gtfs <- function(conf = NULL) {
       ALTER TABLE %s.fare_rules ADD CONSTRAINT fare_rules_route_fk FOREIGN KEY (route_id) REFERENCES %s.routes(route_id); \
       ALTER TABLE %s.stop_times ADD CONSTRAINT stop_times_stops_fk FOREIGN KEY (stop_id) REFERENCES %s.stops(stop_id); \
       ALTER TABLE %s.stop_times ADD CONSTRAINT stop_times_trips_fk FOREIGN KEY (trip_id) REFERENCES %s.trips(trip_id); \
+      ALTER TABLE %s.trips ADD CONSTRAINT trips_shapes_fk FOREIGN KEY (shape_id) REFERENCES %s.shapes(shape_id); \
       ALTER TABLE %s.trips ADD CONSTRAINT trips_calendar_fk FOREIGN KEY (service_id) REFERENCES %s.calendar(service_id); \
-      ALTER TABLE %s.trips ADD CONSTRAINT trips_routes_fk FOREIGN KEY (route_id) REFERENCES %s.routes(route_id);", conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema)
+      ALTER TABLE %s.trips ADD CONSTRAINT trips_routes_fk FOREIGN KEY (route_id) REFERENCES %s.routes(route_id);", conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema)
       cat(q)
       DBI::dbExecute(conn, q)