Skip to content

Commit

Permalink
Merge pull request #58 from evansiroky/download-fix
Browse files Browse the repository at this point in the history
bunch 'o stuff
  • Loading branch information
evansiroky authored Jan 15, 2018
2 parents 4b29ea4 + 4be1c3d commit 442b9b2
Show file tree
Hide file tree
Showing 74 changed files with 549 additions and 143 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ notifications:

node_js:
- '6'
- '5'
- '4'

env:
- COVERAGE=false
Expand Down
105 changes: 45 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Currently works only with PostgreSQL (including PostGIS), MySQL (with spatial ca
## Table of Contents

* [Installation](#installation)
* [Usage](#usage)
* [API](#api)

## Installation

Expand All @@ -19,6 +19,7 @@ In order to use this library, you must also install the additional libraries in

npm install pg --save
npm install pg-copy-streams --save
npm install pg-query-stream --save
npm install pg-hstore --save

#### With pg and node v0.10.x
Expand All @@ -37,66 +38,13 @@ You must also install the package `promise-polyfill` and write additional code.

Usage with SQLite requires that sqlite is installed and is available via a unix command line.

## Usage:
## API:

### Downloading the GTFS File:
### GTFS(options)

```js
var GTFS = require('gtfs-sequelize');

var downloadConfig = {
gtfsUrl: 'http://feed.rvtd.org/googleFeeds/static/google_transit.zip',
downloadsDir: 'downloads'
};

var gtfs = GTFS(downloadConfig);
gtfs.downloadGtfs(function() {
//download has finished callback
});
```

### Loading GTFS into Database:

```js
var GTFS = require('gtfs-sequelize');

var pgConfig = {
database: 'postgres://gtfs_sequelize:gtfs_sequelize@localhost:5432/gtfs-sequelize-test',
downloadsDir: 'downloads',
gtfsFileOrFolder: 'google_transit.zip',
sequelizeOptions: {
logging: false
}
}
Create a new GTFS API.

var gtfs = GTFS(pgConfig);
gtfs.loadGtfs(function() {
//database loading has finished callback
});
```

### Loading into a DB with PostGIS installed:

```js
var GTFS = require('gtfs-sequelize');

var pgConfig = {
database: 'postgres://gtfs_sequelize:gtfs_sequelize@localhost:5432/gtfs-sequelize-test',
downloadsDir: 'downloads',
gtfsFileOrFolder: 'google_transit.zip',
spatial: true,
sequelizeOptions: {
logging: false
}
}

var gtfs = GTFS(pgConfig);
gtfs.loadGtfs(function() {
//database loading has finished callback
});
```

### Querying a specific schema within a DB:
Example:

```js
var GTFS = require('gtfs-sequelize');
Expand All @@ -107,8 +55,7 @@ var pgConfig = {
gtfsFileOrFolder: 'google_transit.zip',
spatial: true,
sequelizeOptions: {
logging: false,
schema: 'test_schema'
logging: false
}
}

Expand All @@ -117,3 +64,41 @@ gtfs.loadGtfs(function() {
//database loading has finished callback
});
```

#### options

| Key | Value |
| -- | -- |
| database | A database connection string. You must specify a user and a database in your connection string. The database must already exist, but the tables within the db do not need to exist. |
| downloadsDir | The directory where you want the feed zip fils downloaded to or where you're going to read the feed read from. |
| gtfsFileOrFolder | The (zip) file or folder to load the gtfs from |
| interpolateStopTimes | Default is undefined. If true, after loading the stop_times table, all stop_times with undefined arrival and departure times will be updated to include interpolated arrival and departure times. |
| sequelizeOptions | Options to pass to sequelize. Note: to use a specific schema you'll want to pass something like this: `{ schema: 'your_schema' }` |
| spatial | Default is undefined. If true, spatial tables for the shapes and stops will be created. |

### gtfs.connectToDatabase()

Return a sequelize api of the database.

Example:

```js
var db = gtfs.connectToDatabase()

db.stop.findAll()
.then(stops => {
console.log(stops)
})
```

### gtfs.downloadGtfs(callback)

If a url is provided, the feed will be attempted to be downloaded. Works with `http`, `https` and `ftp`.

### gtfs.interpolateStopTimes(callback)

Interpolate stop_times with undefined arrival and departure times. If you load a gtfs with the `interpolateStopTimes` flag set to true, you don't need to call this.

### gtfs.loadGtfs(callback)

Load the gtfs into the database.
45 changes: 26 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
var path = require('path'),
downloadGtfs = require('./lib/download.js'),
Database = require('./models'),
loadgtfs = require('./lib/gtfsLoader.js');

module.exports = function(config) {
const downloadGtfs = require('./lib/download.js')
const loadgtfs = require('./lib/gtfsLoader.js')
const operations = require('./lib/operations')
const Database = require('./models')

var connectToDatabase = function(rawModels) {
var db = Database(config.database, config.sequelizeOptions ? config.sequelizeOptions : {});
if(!rawModels && config.spatial) {
db.stop = db.sequelize.import('models/spatial/stop.js');
db.shape_gis = db.sequelize.import('models/spatial/shape_gis.js');
db.trip = db.sequelize.import('models/spatial/trip.js');
module.exports = function (config) {
const connectToDatabase = function (rawModels) {
const db = Database(config.database, config.sequelizeOptions ? config.sequelizeOptions : {})
if (!rawModels && config.spatial) {
db.stop = db.sequelize.import('models/spatial/stop.js')
db.shape_gis = db.sequelize.import('models/spatial/shape_gis.js')
db.trip = db.sequelize.import('models/spatial/trip.js')
// reassociate spatially-enable models
db.stop.associate(db);
db.shape_gis.associate(db);
db.trip.associate(db);
db.stop.associate(db)
db.shape_gis.associate(db)
db.trip.associate(db)
}
return db;
return db
}

var download = function(callback) {
downloadGtfs(config.gtfsUrl, config.downloadsDir, callback);
const download = function (callback) {
downloadGtfs(config.gtfsUrl, config.downloadsDir, callback)
}

var loadGtfs = function(callback) {
const interpolateStopTimes = function (callback) {
const db = connectToDatabase()
operations.interpolateStopTimes(db, callback)
}

const loadGtfs = function (callback) {
loadgtfs(config.downloadsDir,
config.gtfsFileOrFolder,
connectToDatabase(true),
config.spatial,
callback);
config.interpolateStopTimes,
callback)
}

return {
config: config,
connectToDatabase: connectToDatabase,
downloadGtfs: download,
interpolateStopTimes: interpolateStopTimes,
loadGtfs: loadGtfs
}
}
2 changes: 1 addition & 1 deletion lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = function (gtfsUrl, downloadsDir, callback) {
rimraf(dlFile, cb)
},
dl: ['rm', function (results, cb) {
download(gtfsUrl, dlFile)
download(gtfsUrl, downloadsDir, { filename: 'google_transit.zip' })
.then(() => cb())
.catch(cb)
}]
Expand Down
Loading

0 comments on commit 442b9b2

Please sign in to comment.