1 | # MapPLZ-Node
|
2 |
|
3 | [MapPLZ](http://mapplz.com) is a framework to make mapping quick and easy in
|
4 | your favorite language.
|
5 |
|
6 | <img src="https://raw.githubusercontent.com/mapmeld/mapplz-node/master/logo.jpg" width="140"/>
|
7 |
|
8 | ## Getting started
|
9 |
|
10 | MapPLZ consumes many many types of geodata. It can process data for a script or dump
|
11 | it into a database.
|
12 |
|
13 | Adding some data:
|
14 |
|
15 | ```
|
16 | var MapPLZ = require('mapplz').MapPLZ;
|
17 | var mapstore = new MapPLZ();
|
18 |
|
19 | mapstore = new MapPLZ();
|
20 |
|
21 | // add points
|
22 | mapstore.add(40, -70);
|
23 | mapstore.add([40, -70);
|
24 | mapstore.add({ lat: 40, lng: -70 });
|
25 |
|
26 | // assure items are added using callbacks
|
27 | mapstore.add(40, -70, function(err, pt) { });
|
28 | mapstore.add([40, -70], function(err, pt) { });
|
29 |
|
30 | // add lines
|
31 | mapstore.add([[40, -70], [33, -110]]);
|
32 |
|
33 | // add polygons
|
34 | mapstore.add([[[40, -70], [33, -110], [22, -90], [40, -70]]]);
|
35 |
|
36 | // GeoJSON objects or strings
|
37 | mapstore.add({ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] } });
|
38 | mapstore.add('{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] } }');
|
39 |
|
40 | // add properties
|
41 | mapstore.add({ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] }, "properties": { "color": "#0f0" }},
|
42 | function(err, pt) {
|
43 | pt.properties.color == "#0f0";
|
44 | });
|
45 |
|
46 | mapstore.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
|
52 | mapstore.add('POINT(-70 40)');
|
53 | mapstore.add('color,geo\nred,\'{"type":"Feature","geometry":{"type":"Point","coordinates":[-70,40]}}\'');
|
54 |
|
55 | mapcode = "map\n";
|
56 | mapcode += " marker\n";
|
57 | mapcode += " [40, -70]\n";
|
58 | mapcode += " plz\n";
|
59 | mapcode += "plz\n";
|
60 | mapstore.add(mapcode);
|
61 | ```
|
62 |
|
63 | Each feature is returned as a MapItem, which is easy to retrieve data from.
|
64 |
|
65 | ```
|
66 | mapstore.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 |
|
72 | function 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 |
|
89 | You don't need a database to query data with MapPLZ, but you're welcome to use Postgres/PostGIS or MongoDB.
|
90 |
|
91 | MapPLZ simplifies geodata management and queries:
|
92 |
|
93 | ```
|
94 | mapstore.count("", function(err, count) {
|
95 | // count all, return integer
|
96 | });
|
97 | mapstore.query("", function(err, all_mapitems) {
|
98 | // query all, return [ MapItem ]
|
99 | });
|
100 | mapstore.near([lat, lng], 5, function(err, nearest) {
|
101 | // five nearest
|
102 | // can also send GeoJSON, { lat: Number, lng: Number }, or MapItem
|
103 | });
|
104 | mapstore.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
|
110 | mapstore.count({ color: "blue" }, function(err, count) {
|
111 | // count == 1
|
112 | });
|
113 | mapstore.query({ color: "blue" }, function(err, blue_mapitems) {
|
114 | blue_mapitems == [ MapItem ];
|
115 | });
|
116 |
|
117 | // with PostGIS
|
118 | mapstore.count("color = 'blue'", function(err, count) {
|
119 | // count == 1
|
120 | });
|
121 | mapstore.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 | ```
|
130 | var pg = require('pg');
|
131 | var MapPLZ = require('mapplz');
|
132 |
|
133 | var mapstore = new MapPLZ.MapPLZ();
|
134 | var connString = "postgres://postgres:@localhost/travis_postgis";
|
135 |
|
136 | pg.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 | ```
|
146 | var MongoClient = require('mongodb').MongoClient;
|
147 | var MapPLZ = require('mapplz');
|
148 |
|
149 | var mapstore = new MapPLZ.MapPLZ();
|
150 | var connString = "mongodb://localhost:27017/sample";
|
151 |
|
152 | MongoClient.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 |
|
162 | All 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 |
|
171 | Free BSD License
|
172 |
|
\ | No newline at end of file |