Commit d6119719590359a48c796beb2d3b4dde40538e16

  • avatar
  • arvind
  • Tue Mar 26 21:26:08 IST 2013
Major changes
			- Implementing panorama viewer.
			- Removing JSON editing and saving on server side.  Switching to another graph for annotation.
fetch.py
(16 / 66)
  
22from flask import request
33from flask import render_template
44from flask import make_response
5from flask import jsonify
56import logging
67from logging import FileHandler
7import json
88import pymongo
9import os
10import glob
119
10
1211app = Flask(__name__)
1312
1413@app.route('/', methods=['GET'])
1514def index():
16 if request.args.has_key('json'):
17 return render_template('index.html', json=request.args['json'])
15 if request.args.has_key('url'):
16 return render_template('index.html', url=request.args['url'])
1817 else:
1918 return render_template('index.html')
2019
21@app.route('/editor', methods=['GET'])
22def editor():
23 if request.args.has_key('json'):
24 filename = request.args['json']
25 else:
26 filename = 'test.json'
27 filename = os.path.join(os.path.join(os.path.dirname(__file__), 'static'), filename)
28 try:
29 f = open(filename, 'r')
30 except:
31 f = open(os.path.join(os.path.join(os.path.dirname(__file__), 'static'), 'test.json'), 'r')
32 buf = f.read()
33 f.close()
34 return render_template('editor.html', json = buf)
20@app.route('/fetch',methods=['GET'])
21def fetch():
22 connection = pymongo.Connection()
23 db = connection['mural']
24 collection = db['data']
25 ret = {}
26 x = 0
27 for i in collection.find({'url':request.args['url']}):
28 del(i['_id'])
29 ret[x] = i
30 x = x + 1
31 jsonify(ret)
3532
36@app.route('/saveJSON', methods=['POST'])
37def saveJSON():
38 if request.method == 'POST':
39 response = make_response()
40 JSON = request.form['json']
41 filename = os.path.join(os.path.join(os.path.dirname(__file__), 'static'), request.form['filename'])
42 ls = glob.glob(filename)
43 if len(ls) > 0:
44 response.status_code = 409
45 response.status ="409 Conflict"
46 response.data = "The file that you were trying to save already exits, please try a different name."
47 return response
48 f = open(filename, 'w')
49 f.write(JSON)
50 f.close()
51 return response
52 else:
53 response = make_response()
54 response.code = 400
55 return reponse
5633
57@app.route('/history', methods=['GET'])
58def listJSON():
59 path = os.path.join(os.path.join(os.path.dirname(__file__), 'static'), '*.json')
60 ls = glob.glob(path)
61 def sanitize(i):
62 return i.split('/')[-1]
63 ls = map(sanitize, ls)
64 return render_template('history.html', ls=ls)
65
6634#Log the errors, don't depend on apache to log it for you.
6735 fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a')
6836 fil.setLevel(logging.ERROR)
3939
4040if __name__ == "__main__":
4141 app.run(debug=True, host='0.0.0.0')
42
43# #from bson.code import *
44 # #from urllib import unquote_plus
45 # def application(environ, start_response):
46 # status = '200 OK'
47 # response_headers = [('Content-type', 'application/json'),('Access-Control-Allow-Origin', '*')]
48 # start_response(status, response_headers)
49 # c = pymongo.Connection()
50 # db = c['mural']
51 # coll = db['data']
52 # ret = {}
53 # x = 0
54 # for i in coll.find():
55 # del(i['_id'])
56 # ret[x] = i
57 # x = x + 1
58 # #return repr(recieved)
59 # return json.dumps(ret)
  
124124 z.panMapIfOutOfView = true;
125125 map.addPopup(z);
126126}
127function init(){
128 var options = {
129 controls: [],
130 maxExtent: new OpenLayers.Bounds( 0.0, -4668.0, 31110.0, 0.0 ),
131 maxResolution: 128.000000,
132 numZoomLevels: 8,
133 restrictedExtent: new OpenLayers.Bounds( 0.0, -4668.0, 31110.0, 0.0 )
134 };
135 map = new OpenLayers.Map('map', options);
136 $.get(config.indexer+"/fetch", function(data){
137 if (data != undefined)
138 {
139 ans.ans = data;
140 for(var i in ans.ans)
141 {
142 ans.count+=1;
143 makeBoxes(ans.ans[i]);
144 }
127function init(url){
128 box2 = new OpenLayers.Layer.Vector( "Boxes");
129 box3 = new OpenLayers.Layer.Vector( "Boxes");
130 if(url != undefined)
131 {
132 var options = {
133 controls: [],
134 maxExtent: new OpenLayers.Bounds( 0.0, -928.0, 6744.0, 0.0 ),
135 maxResolution: 32.000000,
136 numZoomLevels: 6
137 };
138 map = new OpenLayers.Map('map', options);
145139
146 }
140 $.get(config.indexer+"/fetch",{url:decodeURIComponent(window.location.search.split("=")[1]).replace("\"","","gi")} , function(data){
141 if (data != undefined)
142 {
143 ans.ans = data;
144 for(var i in ans.ans)
145 {
146 ans.count+=1;
147 makeBoxes(ans.ans[i]);
148 }
147149
148 });
150 }
149151
150 buffer0 = new OpenLayers.Layer.TMS( "TMS Layer","static/",
152 });
153
154
155 buffer0 = new OpenLayers.Layer.TMS( "TMS Layer","static/withpiv/",
151156 { url: '', serviceVersion: '.', layername: 'buffer0', alpha: true,
152157 type: 'png', getURL: overlay_getTileURL, buffer:0, singleTile: false
153158 });
154159
155 buffer1 = new OpenLayers.Layer.TMS( "TMS Layer","static/",
160 buffer1 = new OpenLayers.Layer.TMS( "TMS Layer","static/withpiv/",
156161 { url: '', serviceVersion: '.', layername: 'buffer1', alpha: true,
157162 type: 'png', getURL: overlay_getTileURL, buffer:1, singleTile: false
158163 });
159164
160 buffer2 = new OpenLayers.Layer.TMS( "TMS Layer","static/",
165 buffer2 = new OpenLayers.Layer.TMS( "TMS Layer","static/withpiv/",
161166 { url: '', serviceVersion: '.', layername: 'buffer2', alpha: true,
162167 type: 'png', getURL: overlay_getTileURL, buffer:2, singleTile: false
163168 });
164169
165 var layer = new OpenLayers.Layer.TMS( "TMS Layer","static/",
166 { url: '', serviceVersion: '.', layername: '.', alpha: true,
167 type: 'png', getURL: overlay_getTileURL, isBaseLayer:true
170 var layer = new OpenLayers.Layer.TMS( "TMS Layer","static/withpiv/",
171 { url: '', serviceVersion: '.', layername: '.', alpha: true,
172 type: 'png', getURL: overlay_getTileURL, isBaseLayer:true
173 });
174 }
175 else{
176 var options = {
177 controls: [],
178 maxExtent: new OpenLayers.Bounds( 0.0, -4668.0, 31110.0, 0.0 ),
179 maxResolution: 128.000000,
180 numZoomLevels: 8
181 };
182 map = new OpenLayers.Map('map', options);
183 $.get(config.indexer+"/fetch",{url:window.location.href} , function(data){
184 if (data != undefined)
185 {
186 ans.ans = data;
187 for(var i in ans.ans)
188 {
189 ans.count+=1;
190 makeBoxes(ans.ans[i]);
191 }
192
193 }
194
195 });
196
197 buffer0 = new OpenLayers.Layer.TMS( "TMS Layer","static/",
198 { url: '', serviceVersion: '.', layername: 'buffer0', alpha: true,
199 type: 'png', getURL: overlay_getTileURL, buffer:0, singleTile: false
168200 });
201
202 buffer1 = new OpenLayers.Layer.TMS( "TMS Layer","static/",
203 { url: '', serviceVersion: '.', layername: 'buffer1', alpha: true,
204 type: 'png', getURL: overlay_getTileURL, buffer:1, singleTile: false
205 });
206
207 buffer2 = new OpenLayers.Layer.TMS( "TMS Layer","static/",
208 { url: '', serviceVersion: '.', layername: 'buffer2', alpha: true,
209 type: 'png', getURL: overlay_getTileURL, buffer:2, singleTile: false
210 });
211
212 var layer = new OpenLayers.Layer.TMS( "TMS Layer","static/",
213 { url: '', serviceVersion: '.', layername: '.', alpha: true,
214 type: 'png', getURL: overlay_getTileURL, isBaseLayer:true
215 });
216
217
218 addLabel('3759.0000','-1274.33337','Face');
219 addLabel('3719','-1771','Jewelery');
220 addLabel('4263.0000','-1345.33337', 'Video');
221
222 for (var i = 0; i < box_extents.length; i++) {
223 ext = box_extents[i];
224 bounds = OpenLayers.Bounds.fromArray(ext);
225
226 box = new OpenLayers.Feature.Vector(bounds.toGeometry());
227 box2.addFeatures(box);
228 }
229 for (var i = 0; i < box_extents1.length; i++) {
230 ext = box_extents1[i];
231 bounds = OpenLayers.Bounds.fromArray(ext);
232
233 box = new OpenLayers.Feature.Vector(bounds.toGeometry());
234 box3.addFeatures(box);
235 }
236
237 }
169238 boxes = new OpenLayers.Layer.Vector( "Boxes" );
170239 map.addLayers([layer, buffer0, buffer1, buffer2, boxes]);
171240
241 layer.wrapDateLine = true;
172242 boxes.events.register('featureadded', boxes, myfeatureadded);
173 box2 = new OpenLayers.Layer.Vector( "Boxes" );
174 for (var i = 0; i < box_extents.length; i++) {
175 ext = box_extents[i];
176 bounds = OpenLayers.Bounds.fromArray(ext);
177243
178 box = new OpenLayers.Feature.Vector(bounds.toGeometry());
179 box2.addFeatures(box);
180 }
181 box3 = new OpenLayers.Layer.Vector( "Boxes");
182 for (var i = 0; i < box_extents1.length; i++) {
183 ext = box_extents1[i];
184 bounds = OpenLayers.Bounds.fromArray(ext);
185
186 box = new OpenLayers.Feature.Vector(bounds.toGeometry());
187 box3.addFeatures(box);
188 }
189244 map.addLayers([box2, box3]);
190245
191246 selectControl = new OpenLayers.Control.SelectFeature(box3,
258258 select: selectControl
259259 };
260260
261 map.zoomToExtent( mapBounds );
261
262262 for(var key in drawControls){
263263 map.addControl(drawControls[key]);
264264 }
265265 drawControls['select'].activate();
266 addLabel('3759.0000','-1274.33337','Face');
267 addLabel('3719','-1771','Jewelery');
268 addLabel('4263.0000','-1345.33337', 'Video');
269266 map.addControl(new OpenLayers.Control.PanZoomBar());
270267 map.addControl(new OpenLayers.Control.MousePosition());
271268 map.addControl(new OpenLayers.Control.Navigation());
272269 map.addControl(new OpenLayers.Control.KeyboardDefaults());
273270 map.addControl(new OpenLayers.Control.LayerSwitcher());
274 map.zoomToExtent( mapBounds );
271 map.zoomToExtent( options['maxExtent'] );
275272 //document.getElementById('noneToggle').checked = true;
276273}
277274function addLabel(left, top, name)
455455 myJSON[i].text = ' annotated '+window.location.href+"#[top="+myJSON[i].top+",bottom="+myJSON[i].bottom+",left="+myJSON[i].left+",right="+myJSON[i].right+"] as "+myJSON[i].name+" "+jString+" #swtr"; //The string which gets posted as a tweet.
456456 myJSON[i].text = encodeURIComponent(myJSON[i].text);
457457 myJSON[i].user = user; // User need not know the modification to the JSON.
458 myJSON[i].title = 'Annotation for '+window.location.href;
458 if(window.location.search == ""){
459 myJSON[i].title = 'Annotation for '+window.location.href;
460 myJSON[i].url = window.location.href;
461 }
462 else{
463 myJSON[i].title = 'Annotation for '+ decodeURIComponent(window.location.search.split("=")[1]).replace("\"","","gi");
464 myJSON[i].url = decodeURIComponent(window.location.search.split("=")[1]).replace("\"","","gi");
465 }
459466 }
460467 $.post(config.indexer+'/submit', JSON.stringify(myJSON),function(){
461468 $.post(config.postTweetUrl, {"data":JSON.stringify(myJSON)}, function(data) {
  
1<!DOCTYPE html>
2<html>
3<head>
4 <title>Lepakshi Mural - JSON editor</title>
5 <link rel="stylesheet" type="text/css" href="static/bootstrap.css"></link>
6 <meta charset="utf-8" />
7 <style>
8 textarea {
9 width: 500px;
10 }
11 </style>
12</head>
13<body>
14 <p></p><br>
15 <div class="container">
16 <h3> JSON Editor </h3>
17 <div id="json-saved" class="alert alert-success" style="display: none; width: 300px; margin: auto;">
18 <button type="button" class="close" data-dismiss="alert">&times;</button>
19 <b>Success!</b> JSON saved. See <a href="/history">here</a>
20 </div>
21 <div id="json-error" class="alert alert-error" style="display: none; width: 300px; margin: auto;">
22 <button type="button" class="close" data-dismiss="alert">&times;</button>
23 <b>Invalid JSON!</b> Verify at <a href="http://jsonlint.com">jsonlint.com</a>
24 </div>
25 <textarea id="json" rows="30">
26 {{ json }}
27 </textarea>
28 Enter JSON filename
29 <input type="text" id="filename" placeholder="newname.json">
30 <button class="btn" onclick="save();"> Save </button>
31 </div>
32 <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
33 <script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
34 <script type="text/javascript" src="static/bootstrap.js"></script>
35 <script>
36 function save() {
37 var json = $('#json').val();
38 var filename = $('#filename').val();
39 if(!filename.match(/.json$/)) {
40 filename += '.json';
41 }
42 try {
43 json = JSON.parse(json);
44 $.post('/saveJSON', {"json": JSON.stringify(json), "filename": filename}, function(data) {
45 console.log(data);
46 $('#json-saved').show();
47 }, 'json');
48 } catch(e) {
49 $('#json-error').show();
50 }
51 }
52 </script>
53</body>
54</html>
  
1<!DOCTYPE html>
2<html>
3<head>
4 <title>Lepakshi Mural - JSON history</title>
5 <link rel="stylesheet" type="text/css" href="static/bootstrap.css"></link>
6 <meta charset="utf-8" />
7</head>
8<body>
9 <div class="container">
10 <h3> JSON history </h3>
11 <ul>
12 {% for file in ls %}
13 <li><a href=" {{ url_for('editor') }}?{{ file }}">{{ file }}</a></li>
14 {% endfor %}
15 </ul>
16 </div>
17</body>
18</html>
  
11<!DOCTYPE html>
22<html>
3 <head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
3 <meta charset="utf-8">
4 <head>
65 <script type="text/javascript">
76 function annotationTree() {
87 $('#annotation-tree').modal();
1010 <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
1111 <script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
1212 <script type="text/javascript" src="http://backbonejs.org/backbone-min.js"></script>
13 <!-- <script type="text/javascript" src="static/tree.js"></script> -->
13 <script type="text/javascript" src="static/jit.js"></script>
14 <script type="text/javascript" src="static/tree.js"></script>
1415 <style type='text/css'>
1516 /*#annotation-tree {
1617 position:fixed;
2424 z-index:99999;
2525 }*/
2626 </style>
27 <title>Lepakshi Mural</title>
27 <title>Flask Lepakshi Mural</title>
2828 <meta http-equiv='imagetoolbar' content='no'/>
2929 <link rel="stylesheet" type="text/css" href="static/bootstrap.css"></link>
3030 <!--link rel="stylesheet" type="text/css" href="./tree.css"></link-->
3636 #map {border: 1px solid #888;}
3737 </style>
3838 <script src="http://www.openlayers.org/api/2.11/OpenLayers.js" type="text/javascript"></script>
39 <script type="text/javascript" src="static/bootstrap.js"></script>
40 <script type="text/javascript" src="static/jit.js"></script>
41 <script type="text/javascript" src="static/tree.js"></script>
4239 <script type="text/javascript" src="static/myVector.js"></script>
40 <script type="text/javascript" src="static/bootstrap.js"></script>
4341 <script type="text/javascript">
4442 function treeClose() {
4543 $('#annotation-tree').modal('hide');
4848 }
4949 </script>
5050</head>
51<body onload="init();">
51{% if url %}
52 <body onload='init({{ url|string|safe }});'>
53{% else %}
54 <body onload="init();">
55{% endif %}
5256 <!--div id="header"><h1>Lepakshi Mural</h1></div-->
5357 <div class="fluid-container">
5458
8888 <button type="button" class="close" data-dismiss="alert">&times;</button>
8989 <b>Success!</b> Post successfully posted.
9090 </div>
91 <div id="tree-json-loaded" class="alert alert-success" style="display: none; width: 300px; margin: auto;">
92 <button type="button" class="close" data-dismiss="alert">&times;</button>
93 <b>Success!</b> New JSON loaded.
94 </div>
9591
9692 <div class="btn-group">
9793 <button onclick="handler.trigger();" class="btn btn-small">Mural</button>
9894 <button onclick="handler.trigger1();" class="btn btn-small"> Face</button>
9995 <button onclick="handler.trigger2();" class="btn btn-small">Jewellery</button>
10096 </div>
101
10297 <p></p>
103
104 <input type="text" id="tree-json-file" value="old.json">
105 <button class="btn" id="load-tree-json" onclick="loadTreeJSON();">Load JSON</button>
106 <button class="btn" id="edit-tree-json" onclick="editTreeJSON();">Edit JSON</button>
107 <p></p>
10898 <div id="map"></div>
109
11099 <div id="subheader">Generated by <a href="http://www.maptiler.org/">MapTiler</a><a href="http://www.klokan.cz/projects/gdal2tiles/">GDAL2Tiles</a>, Copyright &copy; 2008 <a href="http://www.klokan.cz/">Klokan Petr Pridal</a>, <a href="http://www.gdal.org/">GDAL</a> &amp; <a href="http://www.osgeo.org/">OSGeo</a> <a href="http://code.google.com/soc/">GSoC</a>
111100 <!-- PLEASE, LET THIS NOTE ABOUT AUTHOR AND PROJECT SOMEWHERE ON YOUR WEBSITE, OR AT LEAST IN THE COMMENT IN HTML. THANK YOU -->
112101 </div>
113102
114 <div class="edit-json">
115 <button id="edit-json-btn" class="btn" onclick="editJSON();">Edit JSON to tweet</button>
116 <textarea rows="20" cols="50" id="edit-json" style="display: none;"></textarea>
117 <button class="btn" id="save-json" style="display: none;" onclick="saveJSON();">Save</button>
118 </div>
119
120103 </div>
121104
122105 <script type="text/javascript" >resize();</script>
113113 </div>
114114 </div>
115115 <div class="modal-body">
116 <div id="infovis"> </div>
117 <div id="tree-info" class="well text-info">
118 <ul></ul>
116 <div id="infovis">
117 <div class="well text-info">
118 <ul>
119 <li><b>Character:</b> <span id="character-value"></span></li>
120 <li>
121 <b>Ornament:</b>
122 <ul>
123 <li><b>Material:</b> <span id="material-value"></span></li>
124 <li><b>Jewellery:</b> <span id="jewellery-value"></span></li>
125 </ul>
126 </li>
127 </ul>
128 </div>
119129 </div>
120130 </div>
121131 <div class="modal-footer">
133133 <button class="btn btn-primary" onclick="treeClose();">Save changes</button>
134134 </div>
135135 </div>
136
137 <script type="text/template" id="tree-info-template">
138 <li><b><%= band %></b>: <span id="<%= node %>-value"><%= node %></span></li>
139 </script>
140
141 <script type="text/javascript">
142 function annotationTree() {
143 $('#annotation-tree').modal();
144 $('#edit-json').hide();
145 $('#edit-json-btn').show();
146 }
147 function editJSON() {
148 $('#edit-json').show();
149 $('#save-json').show();
150 $('#edit-json-btn').hide();
151 $('#edit-json').val(JSON.stringify(myJSON));
152 }
153 function saveJSON() {
154 $('#edit-json').hide();
155 $('#save-json').hide();
156 $('#edit-json-btn').show();
157 try {
158 myJSON = JSON.parse($('#edit-json').val());
159 } catch(e) {
160 alert('The JSON you entered is invalid. Please try again');
161 myJSON = [];
162 }
163 }
164 function treeClose() {
165 $('#annotation-tree').modal('hide');
166 $('#tree-info ul li').each(function(idx, elem) {
167 var key = $(elem).find('b').text().toLowerCase();
168 var val = $(elem).find('span').text().toLowerCase();
169 attribs[key] = val;
170 });
171 myJSON.push(attribs);
172 $("#publish").attr("disabled", false);
173 }
174 function loadTreeJSON() {
175 var file = $('#tree-json-file').val();
176 $.getJSON('static/'+file, function(json) {
177 ht.loadJSON(json);
178 ht.refresh();
179 ht.controller.onComplete();
180 $('#tree-json-loaded').show();
181 });
182 }
183 function editTreeJSON() {
184 var json = $('#tree-json-loaded').val();
185 window.open("{{ url_for('editor') }}?json="+json);
186 }
187 </script>
188136</body>
189137</html>