Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin IGARASHI committed Jul 24, 2020
1 parent 7f41794 commit d52d1bc
Show file tree
Hide file tree
Showing 21 changed files with 51 additions and 83 deletions.
7 changes: 6 additions & 1 deletion src/geojson2inp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class geojson2inp {
pumps,
valves
];
layers.forEach(t=>{t.load()})
layers.forEach(t=>{
const isSucceess = t.load();
if (isSucceess === false && ['Coordinates', 'Pipes'].includes(t.name)){
reject('GeoJSON has no any features in Coordinates or Pipes layers.')
}
})

if (fs.existsSync(this.output)) {
fs.unlinkSync(this.output);
Expand Down
8 changes: 1 addition & 7 deletions src/inp/InpBase.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import fs from 'fs';

export default class InpBase{
protected file: string = '';
protected title: string | undefined;

constructor(file: string, title?: string) {
this.file = file;
this.title = title;
}
constructor(protected file: string, protected title?: string) {}

addText(text: string) {
fs.appendFileSync(this.file, text);
Expand Down
4 changes: 1 addition & 3 deletions src/inp/InpCoordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import InpBase from './InpBase';
import { Coordinate } from '../asset';

export default class InpCoordinates extends InpBase {
private coordMap: { [key: string]: Coordinate };
constructor(file: string, coordMap: { [key: string]: Coordinate }) {
constructor(file: string, private coordMap: { [key: string]: Coordinate }) {
super(file, 'COORDINATES')
this.coordMap = coordMap;
}

createHeader() {
Expand Down
4 changes: 1 addition & 3 deletions src/inp/InpCurves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import InpBase from './InpBase';
import { PumpCurve, Pump } from '../asset';

export default class InpPumps extends InpBase {
private pumps: Pump[];
constructor(file: string, pumps: Pump[]) {
constructor(file: string, private pumps: Pump[]) {
super(file, 'CURVES')
this.pumps = pumps;
}

createHeader() {
Expand Down
2 changes: 1 addition & 1 deletion src/inp/InpEnd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import InpBase from './InpBase';

export default class InpEnd extends InpBase {
constructor(file: string) {
constructor(protected file: string) {
super(file, 'END')
}

Expand Down
4 changes: 1 addition & 3 deletions src/inp/InpJunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import InpBase from './InpBase';
import { Coordinate } from '../asset';

export default class InpJunctions extends InpBase {
private coordMap: { [key: string]: Coordinate };
constructor(file: string, coordMap: { [key: string]: Coordinate }) {
constructor(protected file: string, private coordMap: { [key: string]: Coordinate }) {
super(file, 'JUNCTIONS')
this.coordMap = coordMap;
}

createHeader() {
Expand Down
2 changes: 1 addition & 1 deletion src/inp/InpOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import InpBase from './InpBase';

export default class InpTitle extends InpBase {
constructor(file: string) {
constructor(protected file: string) {
super(file)
}

Expand Down
6 changes: 1 addition & 5 deletions src/inp/InpPipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import InpBase from './InpBase';
import { Pipe } from '../asset';

export default class InpPipes extends InpBase {
private pipes: Pipe[];
private idsExcludes: string[];
constructor(file: string, pipes: Pipe[], idsExcludes: string[]) {
constructor(protected file: string, private pipes: Pipe[], private idsExcludes: string[]) {
super(file, 'PIPES')
this.pipes = pipes;
this.idsExcludes = idsExcludes;
}

createHeader() {
Expand Down
4 changes: 1 addition & 3 deletions src/inp/InpPumps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import InpBase from './InpBase';
import { Pump } from '../asset';

export default class InpPumps extends InpBase {
private pumps: Pump[];
constructor(file: string, pumps: Pump[]) {
constructor(protected file: string, private pumps: Pump[]) {
super(file, 'PUMPS')
this.pumps = pumps;
}

createHeader() {
Expand Down
4 changes: 1 addition & 3 deletions src/inp/InpReservoirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import InpBase from './InpBase';
import { Reservoir } from '../asset';

export default class InpReservoirs extends InpBase {
private reservoirs: Reservoir[];
constructor(file: string, reservoirs: Reservoir[]) {
constructor(protected file: string, private reservoirs: Reservoir[]) {
super(file, 'RESERVOIRS')
this.reservoirs = reservoirs;
}

createHeader() {
Expand Down
4 changes: 1 addition & 3 deletions src/inp/InpTanks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import InpBase from './InpBase';
import { Tank } from '../asset';

export default class InpTanks extends InpBase {
private tanks: Tank[];
constructor(file: string, tanks: Tank[]) {
constructor(protected file: string, private tanks: Tank[]) {
super(file, 'TANKS')
this.tanks = tanks;
}

createHeader() {
Expand Down
4 changes: 1 addition & 3 deletions src/inp/InpTitle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import InpBase from './InpBase';

export default class InpTitle extends InpBase {
private projectTitle: string;
constructor(file: string, projectTitle: string) {
constructor(protected file: string, private projectTitle: string) {
super(file, 'TITLE')
this.projectTitle = projectTitle;
}

exportContents() {
Expand Down
4 changes: 1 addition & 3 deletions src/inp/InpValves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import InpBase from './InpBase';
import { Valve } from '../asset';

export default class InpValves extends InpBase {
private valves: Valve[];
constructor(file: string, valves: Valve[]) {
constructor(protected file: string, private valves: Valve[]) {
super(file, 'VALVES')
this.valves = valves;
}

createHeader() {
Expand Down
6 changes: 3 additions & 3 deletions src/layer/Coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { InpJunctions, InpCoordinates } from '../inp';
export default class Coordinates extends LayerBase {
private coordMap: { [key: string]: Coordinate } = {};

constructor(geojsonFile: string) {
super(geojsonFile);
constructor(protected geojsonFile: string) {
super('Coordinates', geojsonFile);
}

load() {
const geojson = this.loadGeoJSON();
if (!geojson){return;}
if (!geojson){return false;}

this.coordMap = {};
geojson.features.forEach((f: GeoJSON.Feature) => {
Expand Down
9 changes: 3 additions & 6 deletions src/layer/LayerBase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import fs from 'fs';

export default class LayerBase {
protected geojsonFile: string;

constructor(geojsonFile: string) {
this.geojsonFile = geojsonFile;
}
constructor(public name: string, protected geojsonFile: string) {}

loadGeoJSON() {
if (fs.existsSync(this.geojsonFile)){
Expand All @@ -15,7 +11,8 @@ export default class LayerBase {
return;
}
if (!(geojson.features && geojson.features.length >0)){
throw new Error(`No features in this GeoJSON file: ${this.geojsonFile}`);
console.log(`No features in this GeoJSON file: ${this.geojsonFile}`);
return;
}
return geojson;
}
Expand Down
8 changes: 3 additions & 5 deletions src/layer/Pipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import { InpPipes } from '../inp';

export default class Pipes extends LayerBase {
private pipes: Pipe[] = [];
private coords: Coordinates;

constructor(geojsonFile: string, coords: Coordinates) {
super(geojsonFile);
this.coords = coords;
constructor(protected geojsonFile: string, private coords: Coordinates) {
super('Pipes', geojsonFile);
}

load() {
Expand All @@ -21,7 +19,7 @@ export default class Pipes extends LayerBase {
}

const geojson = this.loadGeoJSON();
if (!geojson){return;}
if (!geojson){return false;}
geojson.features.forEach((f: GeoJSON.Feature) => {
const pipe_id: string = getProperty(f.properties, 'id');
const pipe_size: number = getProperty(f.properties, 'diameter');
Expand Down
10 changes: 3 additions & 7 deletions src/layer/Pumps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { InpPumps, InpCurves } from '../inp';

export default class Pumps extends LayerBase {
private pumps: Pump[] = [];
private pipes: Pipes;
private coords: Coordinates;

private del_pipes_id: string[] = [];
get_del_pipes_id(){
Expand All @@ -18,15 +16,13 @@ export default class Pumps extends LayerBase {
return this.del_coords_id;
}

constructor(geojsonFile: string, coords: Coordinates, pipes: Pipes) {
super(geojsonFile);
this.coords = coords;
this.pipes = pipes;
constructor(protected geojsonFile: string, private coords: Coordinates, private pipes: Pipes) {
super('Pumps', geojsonFile);
}

load() {
const geojson = this.loadGeoJSON();
if (!geojson){return;}
if (!geojson){return false;}
geojson.features.forEach((f: GeoJSON.Feature) => {
const pump = new Pump(f.properties);
this.pumps.push(pump);
Expand Down
8 changes: 3 additions & 5 deletions src/layer/Reservoirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import {InpReservoirs} from '../inp';

export default class Reservoirs extends LayerBase {
private reservoirs: Reservoir[] = [];
private coords: Coordinates;

constructor(geojsonFile: string, coords: Coordinates) {
super(geojsonFile);
this.coords = coords;
constructor(protected geojsonFile: string, private coords: Coordinates) {
super('Reservoirs', geojsonFile);
}

load() {
const geojson = this.loadGeoJSON();
if (!geojson){return;}
if (!geojson){return false;}
geojson.features.forEach((f: GeoJSON.Feature) => {
let r: Reservoir = new Reservoir(f.properties);
this.reservoirs.push(r);
Expand Down
8 changes: 3 additions & 5 deletions src/layer/Tanks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import { InpTanks } from '../inp';

export default class Tanks extends LayerBase {
private tanks: Tank[] = [];
private coords: Coordinates;

constructor(geojsonFile: string, coords: Coordinates) {
super(geojsonFile);
this.coords = coords;
constructor(geojsonFile: string, private coords: Coordinates) {
super('Tanks', geojsonFile);
}

load() {
const geojson = this.loadGeoJSON();
if (!geojson){return;}
if (!geojson){return false;}
geojson.features.forEach((f: GeoJSON.Feature) => {
let t: Tank = new Tank(f.properties);
this.tanks.push(t);
Expand Down
10 changes: 3 additions & 7 deletions src/layer/Valves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { InpValves } from '../inp';

export default class Valves extends LayerBase {
private valves: Valve[] = [];
private pipes: Pipes;
private coords: Coordinates;

private del_pipes_id: string[] = [];
get_del_pipes_id(){
Expand All @@ -18,15 +16,13 @@ export default class Valves extends LayerBase {
return this.del_coords_id;
}

constructor(geojsonFile: string, coords: Coordinates, pipes: Pipes) {
super(geojsonFile);
this.coords = coords;
this.pipes = pipes;
constructor(protected geojsonFile: string, private coords: Coordinates, private pipes: Pipes) {
super('Valves', geojsonFile);
}

load() {
const geojson = this.loadGeoJSON();
if (!geojson){return;}
if (!geojson){return false;}
geojson.features.forEach((f: GeoJSON.Feature) => {
const valve = new Valve(f.properties);
this.valves.push(valve);
Expand Down
18 changes: 12 additions & 6 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ describe('failed case', (): void => {
expect.assertions(1);
return js2inp.generate().catch(e => {
expect(e).toBe('One of GeoJSON for jucntions and pipes are missing');
fs.unlinkSync(config.output);
if (fs.existsSync(config.output)){
fs.unlinkSync(config.output);
}
});
})

Expand All @@ -79,7 +81,9 @@ describe('failed case', (): void => {
expect.assertions(1);
return js2inp.generate().catch(e => {
expect(e).toBe('One of GeoJSON for jucntions and pipes are missing');
fs.unlinkSync(config.output);
if (fs.existsSync(config.output)){
fs.unlinkSync(config.output);
}
});
})

Expand All @@ -99,11 +103,13 @@ describe('failed case', (): void => {
expect.assertions(1);
return js2inp.generate().catch(e => {
expect(e).toBe('One of GeoJSON for jucntions and pipes are missing');
fs.unlinkSync(config.output);
if (fs.existsSync(config.output)){
fs.unlinkSync(config.output);
}
});
})

test('geojson is NULL', () => {
test('geojson is NULL', async() => {
const config = {
title: 'test',
geojson: {
Expand All @@ -118,9 +124,9 @@ describe('failed case', (): void => {
}

const js2inp = new geojson2inp(config.geojson, config.output, config.title);
// expect.assertions(1);
expect.assertions(1);
return js2inp.generate().catch(e => {
expect(e.message).toMatch(/No features in this GeoJSON file/);
expect(e).toBe('GeoJSON has no any features in Coordinates or Pipes layers.');
if (fs.existsSync(config.output)){
fs.unlinkSync(config.output);
}
Expand Down

0 comments on commit d52d1bc

Please sign in to comment.