UNPKG

4.65 kBMarkdownView Raw
1# MapPLZ-Node
2
3[MapPLZ](http://mapplz.com) is a framework to make mapping quick and easy in
4your favorite language.
5
6<img src="https://raw.githubusercontent.com/mapmeld/mapplz-node/master/logo.jpg" width="140"/>
7
8## Getting started
9
10MapPLZ consumes many many types of geodata. It can process data for a script or dump
11it into a database.
12
13Adding some data:
14
15```
16var MapPLZ = require('mapplz').MapPLZ;
17var mapstore = new MapPLZ();
18
19mapstore = new MapPLZ();
20
21// add points
22mapstore.add(40, -70);
23mapstore.add([40, -70);
24mapstore.add({ lat: 40, lng: -70 });
25
26// assure items are added using callbacks
27mapstore.add(40, -70, function(err, pt) { });
28mapstore.add([40, -70], function(err, pt) { });
29
30// add lines
31mapstore.add([[40, -70], [33, -110]]);
32
33// add polygons
34mapstore.add([[[40, -70], [33, -110], [22, -90], [40, -70]]]);
35
36// GeoJSON objects or strings
37mapstore.add({ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] } });
38mapstore.add('{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] } }');
39
40// add properties
41mapstore.add({ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] }, "properties": { "color": "#0f0" }},
42 function(err, pt) {
43 pt.properties.color == "#0f0";
44 });
45
46mapstore.add({ lat: 40, lng: -70, color: "blue" }, function(err, pt2) {
47 mapstore.add(40, -70, { color: "blue" }, function(err, pt3) {
48 });
49});
50
51// also: WKT, CSV strings, and MapPLZ code
52mapstore.add('POINT(-70 40)');
53mapstore.add('color,geo\nred,\'{"type":"Feature","geometry":{"type":"Point","coordinates":[-70,40]}}\'');
54
55mapcode = "map\n";
56mapcode += " marker\n";
57mapcode += " [40, -70]\n";
58mapcode += " plz\n";
59mapcode += "plz\n";
60mapstore.add(mapcode);
61```
62
63Each feature is returned as a MapItem, which is easy to retrieve data from.
64
65```
66mapstore.add(40, -70, function(err, pt) {
67 mapstore.add([[40, -70], [50, 20]], { "color": "red" }), function(err, line) {
68 UsePtAndLine(pt, line);
69 });
70});
71
72function UsePtAndLine(pt, line) {
73 pt.lat == 40
74 pt.toGeoJson() == '{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] }}'
75
76 line.type == "line"
77 line.path == [[40, -70], [50, 20]]
78 line.properties.color == "red"
79
80 pt.delete();
81 line.delete(function(err){
82 // line is now deleted
83 });
84}
85```
86
87## Queries
88
89You don't need a database to query data with MapPLZ, but you're welcome to use Postgres/PostGIS or MongoDB.
90
91MapPLZ simplifies geodata management and queries:
92
93```
94mapstore.count("", function(err, count) {
95 // count all, return integer
96});
97mapstore.query("", function(err, all_mapitems) {
98 // query all, return [ MapItem ]
99});
100mapstore.near([lat, lng], 5, function(err, nearest) {
101 // five nearest
102 // can also send GeoJSON, { lat: Number, lng: Number }, or MapItem
103});
104mapstore.within([[[40, -70], [50, -80], [30, -80], [40, -70]]], function(err, within) {
105 // all points within this polygon
106 // can also send GeoJSON, { path: [[[]]] }, or MapItem
107});
108
109// without a DB or with MongoDB
110mapstore.count({ color: "blue" }, function(err, count) {
111 // count == 1
112});
113mapstore.query({ color: "blue" }, function(err, blue_mapitems) {
114 blue_mapitems == [ MapItem ];
115});
116
117// with PostGIS
118mapstore.count("color = 'blue'", function(err, count) {
119 // count == 1
120});
121mapstore.query("color = 'blue'", function(err, blue_mapitems) {
122 blue_mapitems == [ MapItem ];
123});
124
125// coming soon! simple near-point and within-polygon queries
126```
127
128### Setting up PostGIS
129```
130var pg = require('pg');
131var MapPLZ = require('mapplz');
132
133var mapstore = new MapPLZ.MapPLZ();
134var connString = "postgres://postgres:@localhost/travis_postgis";
135
136pg.connect(connString, function(err, client, done) {
137 if(!err) {
138 mapstore.database = new MapPLZ.PostGIS();
139 mapstore.database.client = client;
140 }
141});
142```
143
144### Setting up MongoDB
145```
146var MongoClient = require('mongodb').MongoClient;
147var MapPLZ = require('mapplz');
148
149var mapstore = new MapPLZ.MapPLZ();
150var connString = "mongodb://localhost:27017/sample";
151
152MongoClient.connect(connString, function(err, db) {
153 db.collection('mapplz', function(err, collection) {
154 mapstore.database = new MapPLZ.MongoDB();
155 mapstore.database.collection = collection;
156 });
157});
158```
159
160## Dependencies
161
162All are installed when you run ```npm install mapplz```
163
164* <a href="http://coffeescript.org/">coffee-script</a> (MIT license)
165* <a href="https://github.com/manuelbieh/Geolib">geolib</a> (MIT license)
166* <a href="https://github.com/brianc/node-postgres">node-postgres</a> (BSD license)
167* <a href="http://mongodb.github.io/node-mongodb-native/">node-mongodb-native</a> (Apache license)
168
169## License
170
171Free BSD License
172
\No newline at end of file