This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
forked from geosolutions-it/ckanext-dcatapit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jeoquery.js
182 lines (171 loc) · 7.47 KB
/
jeoquery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* jeoQuery v0.4
*
* Copyright 2012, Thomas Haukland
* Dual licensed under the MIT or GPL Version 2 licenses.
*
*/
var jeoquery = (function ($) {
var my = {};
my.defaultData = {
userName: 'demo',
lang: 'en'
};
my.defaultCountryCode = 'US';
my.defaultLanguage = 'en';
my.geoNamesApiServer = 'api.geonames.org';
my.geoNamesProtocol = 'http';
my.featureClass = {
AdministrativeBoundaryFeatures: 'A',
HydrographicFeatures: 'H',
AreaFeatures: 'L',
PopulatedPlaceFeatures: 'P',
RoadRailroadFeatures: 'R',
SpotFeatures: 'S',
HypsographicFeatures: 'T',
UnderseaFeatures: 'U',
VegetationFeatures: 'V'
};
my.getGeoNames = function(method, data, callback) {
var deferred = $.Deferred();
// TODO: validate method(exists), and params
$.ajax({
url: my.geoNamesProtocol + '://' + my.geoNamesApiServer + '/' + method + 'JSON',
dataType: 'jsonp',
data: $.param($.extend({}, my.defaultData, data), true),
success: function(data) {
deferred.resolve(data);
if (!!callback) callback(data);
},
error: function (xhr, textStatus) {
deferred.reject(xhr, textStatus);
alert('Ooops, geonames server returned: ' + textStatus);
}
});
return deferred.promise();
}
function formatDate(date) {
var dateQs = '';
if (date) {
dateQs = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
}
return dateQs;
}
var methods = {
astergdem: {params: ['lat', 'lng'] },
children: {params: ['geonameId', 'maxRows'] },
cities: {params: ['north', 'south', 'east', 'west', 'lang'] },
countryCode: {params: ['lat', 'lng', 'type', 'lang', 'radius'] },
countryInfo: {params: ['country', 'lang'] },
countrySubdivision: {params: ['lat', 'lng', 'level', 'lang', 'radius'] },
earthquakes: {params: ['north', 'south', 'east', 'west', 'date', 'maxRows', 'minMagnitude'] },
findNearby: {params: ['lat', 'lng', 'featureClass', 'featureCode', 'radius', 'style', 'maxRows'] },
findNearbyPlacename: {params: ['lat', 'lng', 'radius', 'style'] },
findNearbyPostalCodes: {params: ['lat', 'lng', 'radius', 'style', 'maxRows', 'country', 'localCountry', 'postalCode'] },
findNearbyStreets: {params: ['lat', 'lng', 'radius', 'maxRows'] },
findNearbyStreetsOSM: {params: ['lat', 'lng'] },
findNearbyWeather: {params: ['lat', 'lng'] },
findNearbyWikipedia: {params: ['lat', 'lng', 'radius', 'maxRows', 'country', 'postalCode'] },
findNearestAddress: {params: ['lat', 'lng'] },
findNearestIntersection: {params: ['lat', 'lng'] },
findNearestIntersectionOSM: {params: ['lat', 'lng', 'radius', 'maxRows'] },
findNearbyPOIsOSM: {params: ['lat', 'lng'] },
get: {params: ['geonameId', 'lang', 'style'] },
gtopo30: {params: ['lat', 'lng'] },
hierarchy: {params: ['geonameId'] },
neighbourhood: {params: ['lat', 'lng'] },
neighbours: {params: ['geonameId', 'country'] },
ocean: {params: ['lat', 'lng', 'radius'] },
postalCodeCountryInfo: {params: [] },
postalCodeLookup: {params: ['postalcode', 'country', 'maxRows', 'charset'] },
postalCodeSearch: {params: ['postalcode', 'postalcode_startsWith', 'placename_startsWith', 'country', 'countryBias', 'maxRows', 'style', 'operator', 'charset', 'isReduced'] },
search: {params: [ 'q', 'name', 'name_equals', 'name_startsWith', 'maxRows', 'startRow', 'country', 'countryBias', 'continentCode', 'adminCode1', 'adminCode2', 'adminCode3', 'featureClass', 'featureCode', 'lang', 'type', 'style', 'isNameRequired', 'tag', 'operator', 'charset', 'fuzzy'] },
siblings: {params: ['geonameId'] },
srtm3: {params: ['lat', 'lng'] },
timezone: {params: ['lat', 'lng', 'radius', 'date'] },
weather: {params: ['north', 'south', 'east', 'west', 'maxRows'] },
weatherIcao: {params: ['ICAO'] },
wikipediaBoundingBox: {params: ['north', 'south', 'east', 'west', 'lang', 'maxRows'] },
wikipediaSearch: {params: ['q', 'title', 'lang', 'maxRows'] }
};
return my;
}(jQuery));
(function ($) {
$.fn.jeoCountrySelect = function (options) {
var el = this;
$.when(jeoquery.getGeoNames('countryInfo'))
.then(function (data) {
var sortedNames = data.geonames;
if (data.geonames.sort) {
sortedNames = data.geonames.sort(function (a, b) {
return a.countryName.localeCompare(b.countryName);
});
}
// Insert blank choice
sortedNames.unshift({countryCode:'', countryName:''});
var html = $.map(sortedNames, function(c) {
return '<option value="' + c.countryCode + '">' + c.countryName + '</option>';
});
el.html(html);
if (options && options.callback) options.callback(sortedNames);
});
};
$.fn.jeoPostalCodeLookup = function (options) {
this.on("change", function () {
var code = $(this).val();
var country = options.country || jeoquery.defaultCountryCode;
if (options.countryInput) {
country = options.countryInput.val() || jeoquery.defaultCountry;
}
if (code) {
jeoquery.getGeoNames('postalCodeLookup', {postalcode: code, country: country}, function (data) {
if (data && data.postalcodes && data.postalcodes.length > 0) {
if (options) {
if (options.target) {
options.target.val(data.postalcodes[0].placeName);
}
if (options.callback) {
options.callback(data.postalcodes[0]);
}
}
}
});
}
});
};
$.fn.jeoCityAutoComplete = function (options) {
this.autocomplete({
source: function (request, response) {
jeoquery.getGeoNames('search', {
featureClass: jeoquery.featureClass.PopulatedPlaceFeatures,
style: "full",
maxRows: 12,
name_startsWith: request.term
}, function (data) {
response(
$.map(data.geonames,
function (item) {
var displayName = item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName;
return {
label: displayName,
value: displayName,
details: item
};
}));
});
},
minLength: 2,
select: function( event, ui ) {
if (ui && ui.item && options && options.callback) {
options.callback(ui.item.details);
}
},
open: function () {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function () {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}
});
};
})(jQuery);