Skip to content

Commit

Permalink
fix: geojson type error
Browse files Browse the repository at this point in the history
  • Loading branch information
codediodeio committed Jul 11, 2018
1 parent b8b77c2 commit 3379be7
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ const users = geo.collection('users', ref =>
const nearbyOnlineUsers = users.within(center, radius, field);
```

### Usage with RxJS < 6.2, or Ionic v3

This package requires RxJS 6.2, but you can still use it with older versions without blowing up you app by installing rxjs-compat.

Example:

```shell
npm i rxjs@latest rxjs-compat
```

### Seeing this error: `DocumentReference.set() called with invalid data`

Firestore writes cannot use custom classes, so make sure to call the `data` getter on the point.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { switchMap, tap, map, take, finalize } from 'rxjs/operators';
import * as firebaseApp from 'firebase/app';
import * as geofirex from 'geofirex';
import { GeoFireCollectionRef } from 'geofirex';
import { Point, Feature } from 'geojson';

@Component({
selector: 'app-realtime-geoquery',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "geofirex",
"version": "0.0.4",
"version": "0.0.5",
"description": "Realtime Firestore GeoQueries with RxJS",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
3 changes: 1 addition & 2 deletions src/geohash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { firestore } from './interfaces';
import { firestore, Point, Feature } from './interfaces';

import { Point, Feature } from 'geojson';
import { neighbors, encode, flip } from './util';

import distance from '@turf/distance';
Expand Down
203 changes: 203 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,206 @@ export namespace firestore {
firestore?(): types.FirebaseFirestore;
}
}

/// GEOJSON

// Type definitions for geojson 7946.0
// Project: https://geojson.org/
// Definitions by: Jacob Bruun <https://github.com/cobster>
// Arne Schubert <https://github.com/atd-schubert>
// Jeff Jacobson <https://github.com/JeffJacobson>
// Ilia Choly <https://github.com/icholy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

// Note: as of the RFC 7946 version of GeoJSON, Coordinate Reference Systems
// are no longer supported. (See https://tools.ietf.org/html/rfc7946#appendix-B)}

/**
* The valid values for the "type" property of GeoJSON geometry objects.
* https://tools.ietf.org/html/rfc7946#section-1.4
*/
export type GeoJsonGeometryTypes =
| 'Point'
| 'LineString'
| 'MultiPoint'
| 'Polygon'
| 'MultiLineString'
| 'MultiPolygon'
| 'GeometryCollection';

/**
* The value values for the "type" property of GeoJSON Objects.
* https://tools.ietf.org/html/rfc7946#section-1.4
*/
export type GeoJsonTypes =
| 'FeatureCollection'
| 'Feature'
| GeoJsonGeometryTypes;

/**
* Bounding box
* https://tools.ietf.org/html/rfc7946#section-5
*/
export type BBox =
| [number, number, number, number]
| [number, number, number, number, number, number];

/**
* A Position is an array of coordinates.
* https://tools.ietf.org/html/rfc7946#section-3.1.1
* Array should contain between two and three elements.
* The previous GeoJSON specification allowed more elements (e.g., which could be used to represent M values),
* but the current specification only allows X, Y, and (optionally) Z to be defined.
*/
export type Position = number[]; // [number, number] | [number, number, number];

/**
* The base GeoJSON object.
* https://tools.ietf.org/html/rfc7946#section-3
* The GeoJSON specification also allows foreign members
* (https://tools.ietf.org/html/rfc7946#section-6.1)
* Developers should use "&" type in TypeScript or extend the interface
* to add these foreign members.
*/
export interface GeoJsonObject {
// Don't include foreign members directly into this type def.
// in order to preserve type safety.
// [key: string]: any;
/**
* Specifies the type of GeoJSON object.
*/
type: GeoJsonTypes;
/**
* Bounding box of the coordinate range of the object's Geometries, Features, or Feature Collections.
* https://tools.ietf.org/html/rfc7946#section-5
*/
bbox?: BBox;
}

/**
* Union of GeoJSON objects.
*/
export type GeoJSON = Geometry | Feature | FeatureCollection;

/**
* A geometry object.
* https://tools.ietf.org/html/rfc7946#section-3
*/
export interface GeometryObject extends GeoJsonObject {
type: GeoJsonGeometryTypes;
}

/**
* Union of geometry objects.
* https://tools.ietf.org/html/rfc7946#section-3
*/
export type Geometry =
| Point
| MultiPoint
| LineString
| MultiLineString
| Polygon
| MultiPolygon
| GeometryCollection;

/**
* Point geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.2
*/
export interface Point extends GeometryObject {
type: 'Point';
coordinates: Position;
}

/**
* MultiPoint geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.3
*/
export interface MultiPoint extends GeometryObject {
type: 'MultiPoint';
coordinates: Position[];
}

/**
* LineString geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.4
*/
export interface LineString extends GeometryObject {
type: 'LineString';
coordinates: Position[];
}

/**
* MultiLineString geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.5
*/
export interface MultiLineString extends GeometryObject {
type: 'MultiLineString';
coordinates: Position[][];
}

/**
* Polygon geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.6
*/
export interface Polygon extends GeometryObject {
type: 'Polygon';
coordinates: Position[][];
}

/**
* MultiPolygon geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.7
*/
export interface MultiPolygon extends GeometryObject {
type: 'MultiPolygon';
coordinates: Position[][][];
}

/**
* Geometry Collection
* https://tools.ietf.org/html/rfc7946#section-3.1.8
*/
export interface GeometryCollection extends GeometryObject {
type: 'GeometryCollection';
geometries: Geometry[];
}

export type GeoJsonProperties = { [name: string]: any } | null;

/**
* A feature object which contains a geometry and associated properties.
* https://tools.ietf.org/html/rfc7946#section-3.2
*/
export interface Feature<
G extends GeometryObject | null = Geometry,
P = GeoJsonProperties
> extends GeoJsonObject {
type: 'Feature';
/**
* The feature's geometry
*/
geometry: G;
/**
* A value that uniquely identifies this feature in a
* https://tools.ietf.org/html/rfc7946#section-3.2.
*/
id?: string | number;
/**
* Properties associated with this feature.
*/
properties: P;
}

/**
* A collection of feature objects.
* https://tools.ietf.org/html/rfc7946#section-3.3
*/
export interface FeatureCollection<
G extends GeometryObject | null = Geometry,
P = GeoJsonProperties
> extends GeoJsonObject {
type: 'FeatureCollection';
features: Array<Feature<G, P>>;
}

0 comments on commit 3379be7

Please sign in to comment.