1 ;;; PHOROS -- Photogrammetric Road Survey
2 ;;; Copyright (C) 2010, 2011, 2012 Bert Burgemeister
4 ;;; This program is free software; you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation; either version 2 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License along
15 ;;; with this program; if not, write to the Free Software Foundation, Inc.,
16 ;;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 (setf *js-target-version
* 1.8)
22 ;;; Debug helpers. TODO: remove them.
23 (defparameter *t
* nil
)
24 (defparameter *tt
* nil
)
26 (cffi:define-foreign-library phoml
27 (:unix
(:or
"./libphoml.so"
28 "./phoml/lib/libphoml.so"))
29 (t (:default
"libphoml")))
31 (defparameter *standard-coordinates
* 4326
32 "EPSG code of the coordinate system that we use for communication.")
34 (defparameter *spherical-mercator
* 900913
35 "EPSG code of the coordinate system used for some distance calculations.")
37 (defvar *verbosity
* nil
38 "List of strings like \"topic:7\".")
40 (defvar *postgresql-credentials
* nil
41 "A list: (database user password host &key (port 5432) use-ssl).")
43 (defvar *postgresql-aux-credentials
* nil
44 "A list: (database user password host &key (port 5432) use-ssl).")
46 (defparameter *photogrammetry-mutex
* (bt:make-lock
"photogrammetry"))
48 (setf *read-default-float-format
* 'double-float
)
50 (defparameter *phoros-server
* nil
"Hunchentoot acceptor.")
52 (defparameter *common-root
* nil
53 "Root directory; contains directories of measuring data.")
55 (defparameter *proxy-root
* "phoros"
56 "First directory element of the server URL. Must correspond to the
57 proxy configuration if Phoros is hidden behind a proxy.")
59 (defparameter *login-intro
* nil
60 "A few friendly words to be shown below the login form.")
62 (defparameter *number-of-images
* 4
63 "Number of photos shown to the HTTP client.")
65 (defparameter *aux-numeric-labels
* nil
66 "Labels for auxiliary numeric data rows shown to the HTTP client.")
68 (defparameter *aux-text-labels
* nil
69 "Labels for auxiliary text data rows shown to the HTTP client.")
71 (defparameter *browser-cache-max-age
* (* 3600 24 7)
72 "Value x for Cache-Control:max-age=x, for images on client.")
74 (defparameter *number-of-features-per-layer
* 500
75 "What we think a browser can swallow.")
77 (defparameter *number-of-points-per-aux-linestring
* 500
78 "What we think a browser can swallow.")
80 (defparameter *user-point-creation-date-format
* "IYYY-MM-DD HH24:MI:SS TZ"
81 "SQL date format used for display and GeoJSON export of user points.")
83 (defparameter *phoros-version
*
84 (asdf:component-version
(asdf:find-system
:phoros
))
85 "Phoros version as defined in system definition.")
87 (defparameter *phoros-description
*
88 (asdf:system-description
(asdf:find-system
:phoros
))
89 "Phoros description as defined in system definition.")
91 (defparameter *phoros-long-description
*
92 (asdf:system-long-description
(asdf:find-system
:phoros
))
93 "Phoros long-description as defined in system definition.")
95 (defparameter *phoros-licence
*
96 (asdf:system-licence
(asdf:find-system
:phoros
))
97 "Phoros licence as defined in system definition.")
99 (defun version-number-parts (dotted-string)
100 "Return the three version number components of something like
103 (values-list (mapcar #'parse-integer
104 (cl-utilities:split-sequence
#\. dotted-string
)))))
106 (defun phoros-version (&key major minor revision
)
107 "Return version of this program, either one integer part as denoted by
108 the key argument, or the whole dotted string."
109 (multiple-value-bind (major-number minor-number revision-number
)
110 (version-number-parts *phoros-version
*)
111 (cond (major major-number
)
113 (revision revision-number
)
114 (t *phoros-version
*))))
116 (defun check-dependencies ()
117 "Say OK if the necessary external dependencies are available."
120 (geographic-to-utm 33 13 52) ;check cs2cs
121 (phoros-photogrammetry:del-all
) ;check photogrammetry
122 (initialize-leap-seconds) ;check source of leap second info
123 (format *error-output
* "~&OK~%"))
124 (error (e) (format *error-output
* "~A~&" e
))))
126 (defun muffle-postgresql-warnings ()
127 "For current DB, silence PostgreSQL's warnings about implicitly
129 (unless (cli:verbosity-level
:postgresql-warnings
)
130 (execute "SET client_min_messages TO ERROR;")))
132 (defun check-db (db-credentials)
133 "Check postgresql connection. Return t if successful; show error on
134 *error-output* otherwise. db-credentials is a list like so: (database
135 user password host &key (port 5432) use-ssl)."
138 (setf connection
(apply #'connect db-credentials
))
139 (error (e) (format *error-output
* "Database connection ~S failed: ~A~&"
142 (disconnect connection
)
145 (defun ignore-warnings (c) (declare (ignore c
)) (muffle-warning))
147 (defmethod hunchentoot:session-cookie-name
(acceptor)
148 (declare (ignore acceptor
))
151 (defun start-server (&key
(proxy-root "phoros") (http-port 8080) address
153 "Start the presentation project server which listens on http-port
154 at address. Address defaults to all addresses of the local machine."
155 (setf *phoros-server
*
156 (make-instance 'hunchentoot
:easy-acceptor
159 :document-root
(ensure-directories-exist
161 :error-template-directory
(ensure-directories-exist
162 "unexpected_html/errors/")))
163 (setf hunchentoot
:*session-max-time
* (* 3600 24))
164 (setf *proxy-root
* proxy-root
)
165 (setf *common-root
* common-root
)
166 (check-db *postgresql-credentials
*)
167 (with-connection *postgresql-credentials
*
168 (assert-phoros-db-major-version))
169 (hunchentoot:reset-session-secret
)
170 (hunchentoot:start
*phoros-server
*))
172 (defun stop-server () (hunchentoot:stop
*phoros-server
*))
174 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
175 (register-sql-operators :2+-ary
:&& :overlaps
))
177 (hunchentoot:define-easy-handler phoros-handler
()
178 "First HTTP contact: if necessary, check credentials, establish new
180 (with-connection *postgresql-credentials
*
181 (let* ((s (cl-utilities:split-sequence
183 (hunchentoot:script-name
*)
184 :remove-empty-subseqs t
))
185 (presentation-project-name (second s
))
186 (presentation-project-id
188 (presentation-project-id-from-name presentation-project-name
))))
190 ;; TODO: remove the following line (which seems to function as a
191 ;; wakeup call of sorts)...
192 (get-dao 'sys-user-role
0 0)
193 ;; ...and make sure the following error doesn't occur any longer
194 ;; while accessing the HTTP server:
195 ;; #<POSTMODERN:DAO-CLASS PHOROS::SYS-USER-ROLE> cannot be printed readably.
198 ((null presentation-project-id
)
199 (setf (hunchentoot:return-code
*) hunchentoot
:+http-not-found
+))
200 ((and (equal (hunchentoot:session-value
'presentation-project-name
)
201 presentation-project-name
)
202 (hunchentoot:session-value
'authenticated-p
))
203 (hunchentoot:redirect
204 (format nil
"/~A/lib/view-~A"
211 (setf (hunchentoot:session-value
'presentation-project-name
)
212 presentation-project-name
)
213 (setf (hunchentoot:session-value
'presentation-project-id
)
214 presentation-project-id
)
215 (setf (hunchentoot:session-value
'presentation-project-bbox
)
218 (bounding-box (get-dao 'sys-presentation-project
219 presentation-project-name
)))))
220 (if (or (null bbox
) (eq :null bbox
))
223 (setf (hunchentoot:session-value
'aux-data-p
)
224 (with-connection *postgresql-aux-credentials
*
225 (view-exists-p (aux-point-view-name
226 presentation-project-name
))))
227 (who:with-html-output-to-string
(s nil
:prologue t
:indent t
)
229 :style
"font-family:sans-serif;"
231 :method
"post" :enctype
"multipart/form-data"
232 :action
(format nil
"/~A/lib/authenticate"
236 (:legend
(:b
(:a
:href
"http://phoros.boundp.org"
237 :style
"text-decoration:none;"
239 (who:fmt
" [~A]" presentation-project-name
)))
241 (:b
(:em
"You can't do much without JavaScript there.")))
244 (:input
:type
"text" :name
"user-name"))
247 (:input
:type
"password" :name
"user-password")
249 (:span
:id
"cackle"))
250 (:input
:type
"submit" :value
"Submit"
252 (setf (chain document
253 (get-element-by-id "cackle")
255 "Ok, let's see…"))))
256 (:script
:type
"text/javascript"
257 (who:str
(ps (chain document
262 for i in
*login-intro
*
263 do
(who:htm
(:p
(who:str i
))))))))))))
265 (pushnew (hunchentoot:create-regex-dispatcher
"/phoros/(?!lib/)"
267 hunchentoot
:*dispatch-table
*)
269 (defun stored-bbox ()
270 "Return stored bounding box for user and presentation project of
272 (with-connection *postgresql-credentials
*
273 (let ((bbox (bounding-box
274 (get-dao 'sys-user-role
275 (hunchentoot:session-value
277 (hunchentoot:session-value
278 'presentation-project-id
)))))
280 (hunchentoot:session-value
'presentation-project-bbox
)
283 (defun stored-cursor ()
284 "Return stored cursor position for user and presentation project of
286 (with-connection *postgresql-credentials
*
289 (:select
(:st_x
'cursor
) (:st_y
'cursor
)
291 :where
(:and
(:= 'user-id
292 (hunchentoot:session-value
'user-id
))
293 (:= 'presentation-project-id
294 (hunchentoot:session-value
295 'presentation-project-id
))
296 (:raw
"cursor IS NOT NULL")))
299 (format nil
"~{~F~#^,~}" cursor
)))))
302 (hunchentoot:define-easy-handler
303 (authenticate-handler :uri
"/phoros/lib/authenticate"
304 :default-request-type
:post
)
306 "Check user credentials."
307 (with-connection *postgresql-credentials
*
308 (let* ((user-name (hunchentoot:post-parameter
"user-name"))
309 (user-password (hunchentoot:post-parameter
"user-password"))
310 (presentation-project-id (hunchentoot:session-value
311 'presentation-project-id
))
313 (when presentation-project-id
316 'sys-user.user-full-name
318 'sys-user-role.user-role
319 :from
'sys-user-role
'sys-user
321 (:= 'presentation-project-id presentation-project-id
)
322 (:= 'sys-user-role.user-id
'sys-user.user-id
)
323 (:= 'user-name user-name
)
324 (:= 'user-password user-password
)))
326 (user-full-name (first user-info
))
327 (user-id (second user-info
))
328 (user-role (third user-info
)))
331 (setf (hunchentoot:session-value
'authenticated-p
) t
332 (hunchentoot:session-value
'user-name
) user-name
333 (hunchentoot:session-value
'user-full-name
) user-full-name
334 (hunchentoot:session-value
'user-id
) user-id
335 (hunchentoot:session-value
'user-role
) user-role
)
336 (hunchentoot:redirect
337 (format nil
"/~A/lib/view-~A"
342 (who:with-html-output-to-string
(s nil
:prologue t
:indent t
)
344 :style
"font-family:sans-serif;"
346 (:a
:href
(format nil
"/~A/~A/"
348 (hunchentoot:session-value
349 'presentation-project-name
))
352 (defun assert-authentication ()
353 "Abort request handler on unauthorized access."
354 (unless (hunchentoot:session-value
'authenticated-p
)
355 (setf (hunchentoot:return-code
*) hunchentoot
:+http-precondition-failed
+)
356 (hunchentoot:abort-request-handler
)))
358 (hunchentoot:define-easy-handler logout-handler
(bbox longitude latitude
)
359 (if (hunchentoot:session-value
'authenticated-p
)
360 (with-connection *postgresql-credentials
*
361 (let ((presentation-project-name
362 (hunchentoot:session-value
'presentation-project-name
))
364 (get-dao 'sys-user-role
365 (hunchentoot:session-value
'user-id
)
366 (hunchentoot:session-value
'presentation-project-id
))))
369 (setf (bounding-box sys-user-role
) bbox
))
370 (when (and longitude latitude
)
371 (let* ;; kludge: should be done by some library, not by DB query
372 ((point-form (format nil
"POINT(~F ~F)" longitude latitude
))
373 (point-wkb (query (:select
374 (:st_geomfromtext point-form
))
376 (setf (cursor sys-user-role
) point-wkb
)))
377 (update-dao sys-user-role
))
378 (hunchentoot:remove-session hunchentoot
:*session
*)
379 (who:with-html-output-to-string
(s nil
:prologue t
:indent t
)
385 "Phoros: logged out" )))
386 (:link
:rel
"stylesheet"
387 :href
(format nil
"/~A/lib/css-~A/style.css"
392 (:h1
:id
"title" "Phoros: logged out")
393 (:p
"Log back in to project "
394 (:a
:href
(format nil
"/~A/~A"
396 presentation-project-name
)
397 (who:fmt
"~A." presentation-project-name
))))))))
400 (pushnew (hunchentoot:create-regex-dispatcher
"/logout" 'logout-handler
)
401 hunchentoot
:*dispatch-table
*)
403 (define-condition superseded
() ()
405 "Tell a thread to finish as soon as possible taking any shortcuts
408 (hunchentoot:define-easy-handler
409 (selectable-restrictions :uri
"/phoros/lib/selectable-restrictions.json"
410 :default-request-type
:post
)
412 "Respond with a list of restrictions the user may choose from."
413 (assert-authentication)
414 (setf (hunchentoot:content-type
*) "application/json")
415 (with-connection *postgresql-credentials
*
416 (json:encode-json-to-string
419 (:select
'restriction-id
420 :from
'sys-selectable-restriction
421 :where
(:= 'presentation-project-id
422 (hunchentoot:session-value
423 'presentation-project-id
)))
427 (defun selected-restrictions (presentation-project-id selected-restriction-ids
)
428 "Get from current database connection a list of restriction clauses
429 belonging to presentation-project-id and ids from list
430 selected-restriction-ids."
433 `(:select
'sql-clause
434 :from
'sys-selectable-restriction
435 :where
(:and
(:= 'presentation-project-id
436 ,presentation-project-id
)
438 ,@(loop for i in selected-restriction-ids
439 collect
(list := 'restriction-id i
))))))
442 (defun sql-where-conjunction (sql-boolean-clauses)
443 "Parenthesize sql-boolean-clauses and concatenate them into a
444 string, separated by \"AND\". Return \" TRUE \" if
445 sql-boolean-clauses is nil."
446 (if sql-boolean-clauses
447 (apply #'concatenate
'string
(butlast (loop
448 for i in sql-boolean-clauses
455 (hunchentoot:define-easy-handler
456 (nearest-image-data :uri
"/phoros/lib/nearest-image-data"
457 :default-request-type
:post
)
459 "Receive coordinates, respond with the count nearest json objects
460 containing picture url, calibration parameters, and car position,
461 wrapped in an array. Wipe away any unfinished business first."
462 (assert-authentication)
463 (dolist (old-thread (hunchentoot:session-value
'recent-threads
))
465 (bt:interrupt-thread old-thread
466 #'(lambda () (signal 'superseded
)))))
467 (setf (hunchentoot:session-value
'recent-threads
) nil
)
468 (setf (hunchentoot:session-value
'number-of-threads
) 1)
469 (push (bt:current-thread
) (hunchentoot:session-value
'recent-threads
))
470 (setf (hunchentoot:content-type
*) "application/json")
471 (with-connection *postgresql-credentials
*
472 (let* ((presentation-project-id (hunchentoot:session-value
473 'presentation-project-id
))
474 (common-table-names (common-table-names
475 presentation-project-id
))
476 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
477 (longitude (cdr (assoc :longitude data
)))
478 (latitude (cdr (assoc :latitude data
)))
479 (count (cdr (assoc :count data
)))
480 (zoom (cdr (assoc :zoom data
)))
481 (snap-distance ;bogus distance in degrees,
482 (* 100e-5 ; assuming geographic
483 (expt 2 (- ; coordinates
484 14 ; (1m = 1e-5 degrees)
487 (point-form (format nil
"POINT(~F ~F)" longitude latitude
))
488 (selected-restrictions-conjunction
489 (sql-where-conjunction
490 (selected-restrictions presentation-project-id
491 (cdr (assoc :selected-restriction-ids
493 (nearest-footprint-centroid-query
494 ;; Inserting the following into
495 ;; image-data-with-footprints-query as a subquery would
496 ;; work correctly but is way too slow.
501 ,@*aggregate-view-columns
*
507 for common-table-name
508 in common-table-names
509 for aggregate-view-name
510 = (aggregate-view-name
516 (:st_centroid
'footprint
)
519 ,*standard-coordinates
*))
521 (:as
(:st_centroid
'footprint
)
523 ,@*aggregate-view-columns
*
527 ;; no-ops wrt self-references in
528 ;; selected-restrictions-conjunction
529 ,@(postmodern-as-clauses
530 (pairlis *aggregate-view-columns
*
531 *aggregate-view-columns
*))
532 :from
',aggregate-view-name
)
533 'images-of-acquisition-project
)
536 (:= 'presentation-project-id
537 ,presentation-project-id
)
542 ,*standard-coordinates
*)
544 (:raw
,selected-restrictions-conjunction
)))))
548 (nearest-footprint-image
549 (ignore-errors (logged-query "centroid of nearest footprint"
550 nearest-footprint-centroid-query
552 (nearest-footprint-centroid
553 (cdr (assoc :centroid nearest-footprint-image
)))
554 (image-data-with-footprints-query
560 for common-table-name in common-table-names
561 for aggregate-view-name
562 = (aggregate-view-name common-table-name
)
565 ,@*aggregate-view-columns
*
566 (:as
(:st_distance
'coordinates
567 ,nearest-footprint-centroid
)
569 (:as
(:not
(:is-null
'footprint
))
571 ,(when (cli:verbosity-level
:render-footprints
)
572 '(:as
(:st_asewkt
'footprint
)
577 ,@(postmodern-as-clauses
578 nearest-footprint-image
)
579 :from
',aggregate-view-name
)
580 'images-of-acquisition-project-plus-reference-image
)
583 (:= 'presentation-project-id
584 ,presentation-project-id
)
585 (:st_contains
'footprint
586 ,nearest-footprint-centroid
)
587 (:raw
,selected-restrictions-conjunction
)))))
590 (nearest-image-without-footprints-query
596 for common-table-name in common-table-names
597 for aggregate-view-name
598 = (aggregate-view-name common-table-name
)
601 ,@*aggregate-view-columns
*
602 (:as
(:st_distance
'coordinates
605 ,*standard-coordinates
*))
607 (:as
(:not
(:is-null
'footprint
))
612 ;; no-ops wrt self-references in
613 ;; selected-restrictions-conjunction
614 ,@(postmodern-as-clauses
615 (pairlis *aggregate-view-columns
*
616 *aggregate-view-columns
*))
617 :from
',aggregate-view-name
)
618 'images-of-acquisition-project
)
620 (:and
(:= 'presentation-project-id
621 ,presentation-project-id
)
622 (:st_dwithin
'coordinates
625 ,*standard-coordinates
*)
627 (:raw
,selected-restrictions-conjunction
)))))
630 (nearest-image-without-footprint
631 (unless nearest-footprint-centroid
;otherwise save time
632 (ignore-errors (logged-query "no footprint, first image"
633 nearest-image-without-footprints-query
635 (image-data-without-footprints-query
641 for common-table-name in common-table-names
642 for aggregate-view-name
643 = (aggregate-view-name common-table-name
)
646 ,@*aggregate-view-columns
*
647 (:as
(:st_distance
'coordinates
650 ,*standard-coordinates
*))
652 (:as
(:not
(:is-null
'footprint
))
657 ,@(postmodern-as-clauses
658 nearest-image-without-footprint
)
659 :from
',aggregate-view-name
)
660 'images-of-acquisition-project
)
662 (:and
(:= 'presentation-project-id
663 ,presentation-project-id
)
664 (:st_dwithin
'coordinates
667 ,*standard-coordinates
*)
669 (:raw
,selected-restrictions-conjunction
)))))
675 (if nearest-footprint-centroid
676 (logged-query "footprints are ready"
677 image-data-with-footprints-query
679 (logged-query "no footprints yet"
680 image-data-without-footprints-query
682 (superseded () nil
))))
683 (when (cli:verbosity-level
:render-footprints
)
687 for photo-parameter-set in result
688 for footprint-vertices
= ;something like this:
689 ;; "SRID=4326;POLYGON((14.334342229 51.723293508 118.492667334,14.334386877 51.723294417 118.404764286,14.334347429 51.72327914 118.506316418,14.334383211 51.723279895 118.435823396,14.334342229 51.723293508 118.492667334))"
690 (ignore-errors ;probably no :footprint-wkt
693 (parse-number:parse-real-number x
))
694 (cl-utilities:split-sequence
#\Space p
)))
696 (cl-utilities:split-sequence-if
701 (cdr (assoc :footprint-wkt photo-parameter-set
)))
704 (if footprint-vertices
708 '(:type
:coordinates
)
712 for footprint-vertex in footprint-vertices
713 for reprojected-vertex
=
716 ;; KLUDGE: translate keys, e.g. a1 -> a_1
717 (json:decode-json-from-string
718 (json:encode-json-to-string photo-parameter-set
))
719 (pairlis '(:x-global
:y-global
:z-global
)
721 (list (proj:degrees-to-radians
722 (first footprint-vertex
))
723 (proj:degrees-to-radians
724 (second footprint-vertex
))
725 (third footprint-vertex
))
727 (cdr (assoc :cartesian-system
728 photo-parameter-set
)))))
730 (list (cdr (assoc :m reprojected-vertex
))
731 (cdr (assoc :n reprojected-vertex
))))))
733 photo-parameter-set
))))
734 (decf (hunchentoot:session-value
'number-of-threads
))
735 (json:encode-json-to-string result
))))
737 (hunchentoot:define-easy-handler
738 (nearest-image-urls :uri
"/phoros/lib/nearest-image-urls"
739 :default-request-type
:post
)
741 "Receive coordinates, respond with a json array of the necessary
742 ingredients for the URLs of the 256 nearest images."
743 (assert-authentication)
744 (push (bt:current-thread
) (hunchentoot:session-value
'recent-threads
))
745 (if (<= (hunchentoot:session-value
'number-of-threads
)
746 0) ;only stuff cache if everything else is done
748 (incf (hunchentoot:session-value
'number-of-threads
))
749 (setf (hunchentoot:content-type
*) "application/json")
750 (with-connection *postgresql-credentials
*
751 (let* ((presentation-project-id (hunchentoot:session-value
752 'presentation-project-id
))
753 (common-table-names (common-table-names
754 presentation-project-id
))
755 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
756 (longitude (cdr (assoc :longitude data
)))
757 (latitude (cdr (assoc :latitude data
)))
759 (radius (* 5d-4
)) ; assuming geographic coordinates
760 (point-form (format nil
"POINT(~F ~F)" longitude latitude
))
768 'directory
'filename
'byte-position
769 'bayer-pattern
'color-raiser
'mounting-angle
775 for common-table-name
776 in common-table-names
777 for aggregate-view-name
778 = (aggregate-view-name common-table-name
)
782 'filename
'byte-position
783 'bayer-pattern
'color-raiser
789 ,*standard-coordinates
*))
792 ',aggregate-view-name
794 (:and
(:= 'presentation-project-id
795 ,presentation-project-id
)
800 ,*standard-coordinates
*)
807 (setf (hunchentoot:return-code
*)
808 hunchentoot
:+http-gateway-time-out
+)
810 (decf (hunchentoot:session-value
'number-of-threads
))
811 (json:encode-json-to-string result
))))
812 (setf (hunchentoot:return-code
*) hunchentoot
:+http-gateway-time-out
+)))
814 (hunchentoot:define-easy-handler
815 (store-point :uri
"/phoros/lib/store-point" :default-request-type
:post
)
817 "Receive point sent by user; store it into database."
818 (assert-authentication)
819 (let* ((presentation-project-name (hunchentoot:session-value
820 'presentation-project-name
))
821 (user-id (hunchentoot:session-value
'user-id
))
822 (user-role (hunchentoot:session-value
'user-role
))
823 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
824 (longitude (cdr (assoc :longitude data
)))
825 (latitude (cdr (assoc :latitude data
)))
826 (ellipsoid-height (cdr (assoc :ellipsoid-height data
)))
827 ;; (stdx-global (cdr (assoc :stdx-global data)))
828 ;; (stdy-global (cdr (assoc :stdy-global data)))
829 ;; (stdz-global (cdr (assoc :stdz-global data)))
830 (input-size (cdr (assoc :input-size data
)))
831 (kind (cdr (assoc :kind data
)))
832 (description (cdr (assoc :description data
)))
833 (numeric-description (cdr (assoc :numeric-description data
)))
835 (format nil
"SRID=4326; POINT(~S ~S ~S)"
836 longitude latitude ellipsoid-height
))
837 (aux-numeric-raw (setf *t
* (cdr (assoc :aux-numeric data
))))
838 (aux-text-raw (cdr (assoc :aux-text data
)))
839 (aux-numeric (if aux-numeric-raw
840 (nullify-nil (apply #'vector aux-numeric-raw
))
842 (aux-text (if aux-text-raw
843 (nullify-nil (apply #'vector aux-text-raw
))
845 (user-point-table-name
846 (user-point-table-name presentation-project-name
)))
848 (not (string-equal user-role
"read")) ;that is, "write" or "admin"
849 () "No write permission.")
850 (with-connection *postgresql-credentials
*
852 (= 1 (execute (:insert-into user-point-table-name
:set
855 'description description
856 'numeric-description numeric-description
857 'creation-date
'current-timestamp
858 'coordinates
(:st_geomfromewkt point-form
)
859 ;; 'stdx-global stdx-global
860 ;; 'stdy-global stdy-global
861 ;; 'stdz-global stdz-global
862 'input-size input-size
863 'aux-numeric aux-numeric
864 'aux-text aux-text
)))
865 () "No point stored. This should not happen."))))
867 (hunchentoot:define-easy-handler
868 (update-point :uri
"/phoros/lib/update-point" :default-request-type
:post
)
870 "Update point sent by user in database."
871 (assert-authentication)
872 (let* ((presentation-project-name (hunchentoot:session-value
873 'presentation-project-name
))
874 (user-id (hunchentoot:session-value
'user-id
))
875 (user-role (hunchentoot:session-value
'user-role
))
876 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
877 (user-point-id (cdr (assoc :user-point-id data
)))
878 (kind (cdr (assoc :kind data
)))
879 (description (cdr (assoc :description data
)))
880 (numeric-description (cdr (assoc :numeric-description data
)))
881 (user-point-table-name
882 (user-point-table-name presentation-project-name
)))
884 (not (string-equal user-role
"read")) ;that is, "write" or "admin"
885 () "No write permission.")
886 (with-connection *postgresql-credentials
*
889 (:update user-point-table-name
:set
892 'description description
893 'numeric-description numeric-description
894 'creation-date
'current-timestamp
895 :where
(:and
(:= 'user-point-id user-point-id
)
896 (:or
(:= (if (string-equal user-role
907 () "No point stored. Did you try to update someone else's point ~
908 without having admin permission?"))))
910 (defun increment-numeric-string (text)
911 "Increment rightmost numeric part of text if any; otherwise append a
912 three-digit numeric part."
913 (let* ((end-of-number
914 (1+ (or (position-if #'digit-char-p text
:from-end t
)
915 (1- (length text
)))))
917 (1+ (or (position-if-not #'digit-char-p text
:from-end t
920 (width-of-number (- end-of-number start-of-number
))
921 (prefix-text (subseq text
0 start-of-number
))
922 (suffix-text (subseq text end-of-number
)))
923 (when (zerop width-of-number
)
924 (setf width-of-number
3))
925 (format nil
"~A~V,'0D~A"
928 (1+ (or (ignore-errors
931 :start start-of-number
:end end-of-number
))
935 (hunchentoot:define-easy-handler
936 (uniquify-point-attributes :uri
"/phoros/lib/uniquify-point-attributes"
937 :default-request-type
:post
)
939 "Check if received set of point-attributes are unique. If so,
940 return null; otherwise return (as a suggestion) a uniquified version
941 of point-attributes by modifying element numeric-description."
942 (assert-authentication)
943 (setf (hunchentoot:content-type
*) "application/json")
944 (let* ((presentation-project-name (hunchentoot:session-value
945 'presentation-project-name
))
946 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
947 (user-point-id (cdr (assoc :user-point-id data
)))
948 (kind (cdr (assoc :kind data
)))
949 (description (cdr (assoc :description data
)))
950 (numeric-description (cdr (assoc :numeric-description data
)))
951 (user-point-table-name
952 (user-point-table-name presentation-project-name
)))
953 (flet ((uniquep (user-point-id kind description numeric-description
)
954 "Check if given set of user-point attributes will be
963 :from user-point-table-name
964 :where
(:and
(:!= 'user-point-id user-point-id
)
966 (:= 'description description
)
967 (:= 'numeric-description
968 numeric-description
)))))
975 :from user-point-table-name
976 :where
(:and
(:= 'kind kind
)
977 (:= 'description description
)
978 (:= 'numeric-description
979 numeric-description
)))))
981 (with-connection *postgresql-credentials
*
982 (json:encode-json-to-string
984 user-point-id kind description numeric-description
)
986 for s
= numeric-description
987 then
(increment-numeric-string s
)
988 until
(uniquep user-point-id kind description s
)
990 (setf (cdr (assoc :numeric-description data
))
994 (hunchentoot:define-easy-handler
995 (delete-point :uri
"/phoros/lib/delete-point" :default-request-type
:post
)
997 "Delete user point if user is allowed to do so."
998 (assert-authentication)
999 (let* ((presentation-project-name (hunchentoot:session-value
1000 'presentation-project-name
))
1001 (user-id (hunchentoot:session-value
'user-id
))
1002 (user-role (hunchentoot:session-value
'user-role
))
1003 (user-point-table-name
1004 (user-point-table-name presentation-project-name
))
1005 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
))))
1006 (with-connection *postgresql-credentials
*
1008 (eql 1 (cond ((string-equal user-role
"admin")
1009 (execute (:delete-from user-point-table-name
1010 :where
(:= 'user-point-id data
))))
1011 ((string-equal user-role
"write")
1014 user-point-table-name
1016 (:= 'user-point-id data
)
1017 (:or
(:= 'user-id user-id
)
1024 () "No point deleted. This should not happen."))))
1026 (defun common-table-names (presentation-project-id)
1027 "Return a list of common-table-names of table sets that contain data
1028 of presentation project with presentation-project-id."
1031 (:select
'common-table-name
1033 :from
'sys-presentation
'sys-measurement
'sys-acquisition-project
1035 (:= 'sys-presentation.presentation-project-id
1036 presentation-project-id
)
1037 (:= 'sys-presentation.measurement-id
1038 'sys-measurement.measurement-id
)
1039 (:= 'sys-measurement.acquisition-project-id
1040 'sys-acquisition-project.acquisition-project-id
)))
1045 "While fetching common-table-names of presentation-project-id ~D: ~A"
1046 presentation-project-id c
))))
1048 (defun encode-geojson-to-string (features &key junk-keys
)
1049 "Encode a list of property lists into a GeoJSON FeatureCollection.
1050 Each property list must contain keys for coordinates, :x, :y, :z; it
1051 may contain a numeric point :id and zero or more pieces of extra
1052 information. The extra information is stored as GeoJSON Feature
1053 properties. Exclude property list elements with keys that are in
1055 (with-output-to-string (s)
1056 (json:with-object
(s)
1057 (json:encode-object-member
:type
:*feature-collection s
)
1058 (json:as-object-member
(:features s
)
1059 (json:with-array
(s)
1061 #'(lambda (point-with-properties)
1062 (dolist (junk-key junk-keys
)
1063 (remf point-with-properties junk-key
))
1064 (destructuring-bind (&key x y z id
&allow-other-keys
) ;TODO: z probably bogus
1065 point-with-properties
1066 (json:as-array-member
(s)
1067 (json:with-object
(s)
1068 (json:encode-object-member
:type
:*feature s
)
1069 (json:as-object-member
(:geometry s
)
1070 (json:with-object
(s)
1071 (json:encode-object-member
:type
:*point s
)
1072 (json:as-object-member
(:coordinates s
)
1073 (json:encode-json
(list x y z
) s
))))
1074 (json:encode-object-member
:id id s
)
1075 (json:as-object-member
(:properties s
)
1076 (dolist (key '(:x
:y
:z
:id
))
1077 (remf point-with-properties key
))
1078 (json:encode-json-plist point-with-properties s
))))))
1080 (json:encode-object-member
:phoros-version
(phoros-version) s
))))
1083 "Return a WKT-compliant BOX3D string from string bbox."
1084 (concatenate 'string
"BOX3D("
1085 (substitute #\Space
#\
,
1086 (substitute #\Space
#\
, bbox
:count
1)
1087 :from-end t
:count
1)
1090 (hunchentoot:define-easy-handler
(points :uri
"/phoros/lib/points.json") (bbox)
1091 "Send a bunch of GeoJSON-encoded points from inside bbox to client."
1092 (assert-authentication)
1093 (setf (hunchentoot:content-type
*) "application/json")
1095 (with-connection *postgresql-credentials
*
1096 (let* ((presentation-project-id
1097 (hunchentoot:session-value
'presentation-project-id
))
1099 (common-table-names presentation-project-id
)))
1100 (encode-geojson-to-string
1107 for common-table-name in common-table-names
1108 for aggregate-view-name
1109 = (point-data-table-name common-table-name
)
1110 ;; would have been nice, was too slow:
1111 ;; = (aggregate-view-name common-table-name)
1114 (:as
(:st_x
'coordinates
) x
)
1115 (:as
(:st_y
'coordinates
) y
)
1116 (:as
(:st_z
'coordinates
) z
)
1117 (:as
'point-id
'id
) ;becomes fid on client
1119 :distinct-on
'random
1120 :from
',aggregate-view-name
1121 :natural
:left-join
'sys-presentation
1124 (:= 'presentation-project-id
1125 ,presentation-project-id
)
1128 (:st_setsrid
(:type
,(box3d bbox
) box3d
)
1129 ,*standard-coordinates
*))))))
1131 ,*number-of-features-per-layer
*))
1133 :junk-keys
'(:random
))))
1136 :error
"While fetching points from inside bbox ~S: ~A"
1139 (hunchentoot:define-easy-handler
1140 (aux-points :uri
"/phoros/lib/aux-points.json")
1142 "Send a bunch of GeoJSON-encoded points from inside bbox to client."
1143 (assert-authentication)
1144 (setf (hunchentoot:content-type
*) "application/json")
1146 (let ((limit *number-of-features-per-layer
*)
1148 (aux-point-view-name (hunchentoot:session-value
1149 'presentation-project-name
))))
1150 (encode-geojson-to-string
1151 (with-connection *postgresql-aux-credentials
*
1157 (:as
(:st_x
'coordinates
) 'x
)
1158 (:as
(:st_y
'coordinates
) 'y
)
1159 (:as
(:st_z
'coordinates
) 'z
)
1160 :from
,aux-view-name
1163 (:st_setsrid
(:type
,(box3d bbox
) box3d
)
1164 ,*standard-coordinates
*)))
1170 :error
"While fetching aux-points from inside bbox ~S: ~A"
1173 (hunchentoot:define-easy-handler
1174 (aux-local-data :uri
"/phoros/lib/aux-local-data"
1175 :default-request-type
:post
)
1177 "Receive coordinates, respond with the count nearest json objects
1178 containing arrays aux-numeric, aux-text, and distance to the
1179 coordinates received, wrapped in an array."
1180 (assert-authentication)
1181 (setf (hunchentoot:content-type
*) "application/json")
1182 (let* ((aux-view-name
1183 (aux-point-view-name (hunchentoot:session-value
1184 'presentation-project-name
)))
1185 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
1186 (longitude (cdr (assoc :longitude data
)))
1187 (latitude (cdr (assoc :latitude data
)))
1188 (count (cdr (assoc :count data
)))
1190 (format nil
"POINT(~F ~F)" longitude latitude
))
1191 (snap-distance 1e-3) ;about 100 m, TODO: make this a defparameter
1193 (format nil
"~A,~A,~A,~A"
1194 (- longitude snap-distance
)
1195 (- latitude snap-distance
)
1196 (+ longitude snap-distance
)
1197 (+ latitude snap-distance
))))
1198 (encode-geojson-to-string
1200 (with-connection *postgresql-aux-credentials
*
1207 (:as
(:st_x
'coordinates
) 'x
)
1208 (:as
(:st_y
'coordinates
) 'y
)
1209 (:as
(:st_z
'coordinates
) 'z
)
1216 ,*spherical-mercator
*)
1218 (:st_geomfromtext
,point-form
,*standard-coordinates
*)
1219 ,*spherical-mercator
*))
1221 :from
',aux-view-name
1222 :where
(:&& 'coordinates
1224 ,(box3d bounding-box
) box3d
)
1225 ,*standard-coordinates
*)))
1230 (defun nillify-null (x)
1231 "Replace occurences of :null in nested sequence x by nil."
1232 (cond ((eq :null x
) nil
)
1236 (t (map (type-of x
) #'nillify-null x
))))
1238 (defun nullify-nil (x)
1239 "Replace occurences of nil in nested sequence x by :null."
1240 (cond ((null x
) :null
)
1244 (t (map (type-of x
) #'nullify-nil x
))))
1246 (hunchentoot:define-easy-handler
1247 (aux-local-linestring :uri
"/phoros/lib/aux-local-linestring.json"
1248 :default-request-type
:post
)
1250 "Receive longitude, latitude, radius, and step-size; respond
1251 with a JSON object comprising the elements linestring (a WKT
1252 linestring stitched together of the nearest auxiliary points from
1253 within radius around coordinates), current-point (the point on
1254 linestring closest to coordinates), and previous-point and next-point
1255 \(points on linestring step-size before and after current-point
1256 respectively). Wipe away any unfinished business first."
1257 (assert-authentication)
1258 (dolist (old-thread (hunchentoot:session-value
'recent-threads
))
1260 (bt:interrupt-thread old-thread
1261 #'(lambda () (signal 'superseded
)))))
1262 (setf (hunchentoot:session-value
'recent-threads
) nil
)
1263 (setf (hunchentoot:session-value
'number-of-threads
) 1)
1264 (push (bt:current-thread
) (hunchentoot:session-value
'recent-threads
))
1265 (setf (hunchentoot:content-type
*) "application/json")
1267 (let* ((thread-aux-points-function-name
1268 (thread-aux-points-function-name (hunchentoot:session-value
1269 'presentation-project-name
)))
1270 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
1271 (longitude (cdr (assoc :longitude data
)))
1272 (latitude (cdr (assoc :latitude data
)))
1273 (radius (cdr (assoc :radius data
)))
1274 (step-size (cdr (assoc :step-size data
)))
1275 (azimuth (if (numberp (cdr (assoc :azimuth data
)))
1276 (cdr (assoc :azimuth data
))
1279 (format nil
"POINT(~F ~F)" longitude latitude
))
1282 (with-connection *postgresql-aux-credentials
*
1287 (,thread-aux-points-function-name
1289 ,point-form
,*standard-coordinates
*)
1291 ,*number-of-points-per-aux-linestring
*
1294 ,(proj:degrees-to-radians
91))))
1296 (with-output-to-string (s)
1297 (json:with-object
(s)
1298 (json:encode-object-member
1299 :linestring
(getf sql-response
:threaded-points
) s
)
1300 (json:encode-object-member
1301 :current-point
(getf sql-response
:current-point
) s
)
1302 (json:encode-object-member
1303 :previous-point
(getf sql-response
:back-point
) s
)
1304 (json:encode-object-member
1305 :next-point
(getf sql-response
:forward-point
) s
)
1306 (json:encode-object-member
1307 :azimuth
(getf sql-response
:new-azimuth
) s
))))
1309 ;; (setf (hunchentoot:return-code*) hunchentoot:+http-gateway-time-out+)
1312 (defun get-user-points (user-point-table-name &key
1313 (bounding-box "-180,-90,180,90")
1315 (order-criterion 'id
)
1317 "Return limit points from user-point-table-name in GeoJSON format,
1318 and the number of points returned."
1319 (let ((user-point-plist
1325 (:as
(:st_x
'coordinates
) 'x
)
1326 (:as
(:st_y
'coordinates
) 'y
)
1327 (:as
(:st_z
'coordinates
) 'z
)
1328 (:as
'user-point-id
'id
) ;becomes fid in OpenLayers
1329 ;; 'stdx-global 'stdy-global 'stdz-global
1331 'kind
'description
'numeric-description
1333 (:as
(:to-char
'creation-date
1334 ,*user-point-creation-date-format
*)
1336 'aux-numeric
'aux-text
1337 :from
,user-point-table-name
:natural
:left-join
'sys-user
1338 :where
(:&& 'coordinates
1339 (:st_setsrid
(:type
,(box3d bounding-box
) box3d
)
1340 ,*standard-coordinates
*)))
1347 (encode-geojson-to-string (nillify-null user-point-plist
)))
1348 (encode-geojson-to-string (nillify-null user-point-plist
)))
1349 (length user-point-plist
))))
1351 (hunchentoot:define-easy-handler
1352 (user-points :uri
"/phoros/lib/user-points.json")
1354 "Send *number-of-features-per-layer* randomly chosen GeoJSON-encoded
1355 points from inside bbox to client. If there is no bbox parameter,
1356 send all points and indent GeoJSON to make it more readable."
1357 (assert-authentication)
1358 (setf (hunchentoot:content-type
*) "application/json")
1360 (let ((bounding-box (or bbox
"-180,-90,180,90"))
1362 (limit (if bbox
*number-of-features-per-layer
* :null
))
1363 (order-criterion (if bbox
'(:random
) 'id
))
1364 (user-point-table-name
1365 (user-point-table-name (hunchentoot:session-value
1366 'presentation-project-name
))))
1367 (with-connection *postgresql-credentials
*
1368 (nth-value 0 (get-user-points user-point-table-name
1369 :bounding-box bounding-box
1371 :order-criterion order-criterion
1375 :error
"While fetching user-points~@[ from inside bbox ~S~]: ~A"
1378 (hunchentoot:define-easy-handler
1379 (user-point-attributes :uri
"/phoros/lib/user-point-attributes.json")
1381 "Send JSON object comprising arrays kinds and descriptions,
1382 each containing unique values called kind and description
1383 respectively, and count being the frequency of value in the user point
1385 (assert-authentication)
1386 (setf (hunchentoot:content-type
*) "application/json")
1388 (let ((user-point-table-name
1389 (user-point-table-name (hunchentoot:session-value
1390 'presentation-project-name
))))
1391 (with-connection *postgresql-credentials
*
1392 (with-output-to-string (s)
1393 (json:with-object
(s)
1394 (json:as-object-member
(:descriptions s
)
1395 (json:with-array
(s)
1396 (mapcar #'(lambda (x) (json:as-array-member
(s)
1397 (json:encode-json-plist x s
)))
1401 (:select
'description
1402 (:count
'description
)
1403 :from user-point-table-name
1404 :group-by
'description
)
1408 (json:as-object-member
(:kinds s
)
1409 (json:with-array
(s)
1410 (mapcar #'(lambda (x) (json:as-array-member
(s)
1411 (json:encode-json-plist x s
)))
1412 (query (format nil
"~
1413 (SELECT kind, count(kind) ~
1414 FROM ((SELECT kind FROM ~A) ~
1417 FROM (VALUES ('solitary'), ~
1420 AS defaults(kind))) ~
1421 AS kinds_union(kind) ~
1423 ORDER BY kind LIMIT 100"
1424 ;; Counts of solitary,
1425 ;; polyline, polygon may be
1426 ;; too big by one if we
1427 ;; collect them like this.
1428 (s-sql:to-sql-name user-point-table-name
))
1432 :error
"While fetching user-point-attributes: ~A"
1435 (hunchentoot:define-easy-handler photo-handler
1436 ((bayer-pattern :init-form
"65280,16711680")
1437 (color-raiser :init-form
"1,1,1")
1438 (mounting-angle :init-form
"0")
1440 "Serve an image from a .pictures file."
1441 (assert-authentication)
1445 (push (bt:current-thread
)
1446 (hunchentoot:session-value
'recent-threads
))
1447 (incf (hunchentoot:session-value
'number-of-threads
)))
1449 (cl-utilities:split-sequence
#\
/
1450 (hunchentoot:script-name
*)
1451 :remove-empty-subseqs t
))
1453 (cdddr ;remove leading phoros, lib, photo
1456 (cl-utilities:split-sequence
#\.
(first (last s
2))))
1458 (parse-integer (car (last s
)) :junk-allowed t
))
1463 :directory
(append (pathname-directory *common-root
*)
1466 :name
(first file-name-and-type
)
1467 :type
(second file-name-and-type
)))))
1469 (flex:with-output-to-sequence
(stream)
1471 stream path-to-file byte-position
1473 (apply #'vector
(mapcar
1475 (cl-utilities:split-sequence
1476 #\
, bayer-pattern
)))
1478 (apply #'vector
(mapcar
1479 #'parse-number
:parse-positive-real-number
1480 (cl-utilities:split-sequence
1483 :reversep
(= 180 (parse-integer mounting-angle
))
1484 :brightenp brightenp
))))
1485 (setf (hunchentoot:header-out
'cache-control
)
1486 (format nil
"max-age=~D" *browser-cache-max-age
*))
1487 (setf (hunchentoot:content-type
*) "image/png")
1489 (decf (hunchentoot:session-value
'number-of-threads
)))
1491 (setf (hunchentoot:return-code
*) hunchentoot
:+http-gateway-time-out
+)
1495 :error
"While serving image ~S: ~A" (hunchentoot:request-uri
*) c
))))
1497 (pushnew (hunchentoot:create-prefix-dispatcher
"/phoros/lib/photo"
1499 hunchentoot
:*dispatch-table
*)
1501 ;;; for debugging; this is the multi-file OpenLayers
1502 (pushnew (hunchentoot:create-folder-dispatcher-and-handler
1503 "/phoros/lib/openlayers/" "OpenLayers-2.10/")
1504 hunchentoot
:*dispatch-table
*)
1506 (pushnew (hunchentoot:create-folder-dispatcher-and-handler
1507 "/phoros/lib/ol/" "ol/")
1508 hunchentoot
:*dispatch-table
*)
1510 (pushnew (hunchentoot:create-folder-dispatcher-and-handler
1511 "/phoros/lib/public_html/" "public_html/")
1512 hunchentoot
:*dispatch-table
*)
1514 (pushnew (hunchentoot:create-static-file-dispatcher-and-handler
1515 "/favicon.ico" "public_html/favicon.ico")
1516 hunchentoot
:*dispatch-table
*)
1518 (hunchentoot:define-easy-handler
1519 (view :uri
(format nil
"/phoros/lib/view-~A" (phoros-version))
1520 :default-request-type
:post
)
1522 "Serve the client their main workspace."
1524 (hunchentoot:session-value
'authenticated-p
)
1525 (who:with-html-output-to-string
(s nil
:prologue t
:indent t
)
1531 "Phoros: " (hunchentoot:session-value
1532 'presentation-project-name
))))
1533 (if (cli:verbosity-level
:use-multi-file-openlayers
)
1536 :src
(format nil
"/~A/lib/openlayers/lib/Firebug/firebug.js"
1539 :src
(format nil
"/~A/lib/openlayers/lib/OpenLayers.js"
1543 :src
(format nil
"/~A/lib/ol/OpenLayers.js"
1545 (:link
:rel
"stylesheet"
1546 :href
(format nil
"/~A/lib/css-~A/style.css"
1550 (:script
:src
(format ;variability in script name is
1551 nil
; supposed to fight browser cache
1552 "/~A/lib/phoros-~A-~A-~A.js"
1555 (hunchentoot:session-value
'user-name
)
1556 (hunchentoot:session-value
'presentation-project-name
)))
1557 (:script
:src
"http://maps.google.com/maps/api/js?sensor=false"))
1560 (:noscript
(:b
(:em
"You can't do much without JavaScript here.")))
1563 "Phoros: " (who:str
(hunchentoot:session-value
'user-full-name
))
1564 (who:fmt
" (~A)" (hunchentoot:session-value
'user-name
))
1565 "with " (:span
:id
"user-role"
1566 (who:str
(hunchentoot:session-value
'user-role
)))
1568 (:span
:id
"presentation-project-name"
1569 (who:str
(hunchentoot:session-value
1570 'presentation-project-name
)))
1571 (:span
:id
"presentation-project-emptiness")
1572 (:span
:id
"recommend-fresh-login")
1573 (:span
:class
"h1-right"
1574 (:span
:id
"caching-indicator")
1575 (:span
:id
"phoros-version"
1576 (who:fmt
"v~A" (phoros-version)))))
1577 ;; streetmap area (northwest)
1579 :class
"controlled-streetmap"
1580 (:div
:id
"streetmap" :class
"streetmap" :style
"cursor:crosshair")
1581 (:div
:id
"streetmap-controls" :class
"streetmap-controls"
1582 (:div
:id
"streetmap-vertical-strut"
1583 :class
"streetmap-vertical-strut")
1584 (:div
:id
"streetmap-layer-switcher"
1585 :class
"streetmap-layer-switcher")
1586 (:button
:id
"unselect-all-restrictions-button"
1588 :onclick
(ps-inline (unselect-all-restrictions))
1590 (:select
:id
"restriction-select"
1591 :name
"restriction-select"
1594 :onchange
(ps-inline (request-photos)))
1595 (:div
:id
"streetmap-overview" :class
"streetmap-overview")
1596 (:div
:id
"streetmap-mouse-position"
1597 :class
"streetmap-mouse-position")
1598 (:div
:id
"streetmap-zoom" :class
"streetmap-zoom")))
1599 ;; control area (north)
1601 :class
"phoros-controls" :id
"phoros-controls"
1602 (:div
:id
"real-phoros-controls"
1603 (:h2
:class
"point-creator h2-phoros-controls"
1605 (:h2
:class
"point-editor h2-phoros-controls"
1607 (:span
:id
"creator"))
1608 (:h2
:class
"point-viewer h2-phoros-controls"
1610 (:span
:id
"creator"))
1611 (:h2
:class
"aux-data-viewer h2-phoros-controls"
1612 "View Auxiliary Data")
1613 (:h2
:class
"multiple-points-viewer"
1614 "Multiple Points Selected")
1615 (:div
:class
"multiple-points-viewer"
1616 (:p
"You have selected multiple user points.")
1617 (:p
"Unselect all but one to edit or view its properties."))
1618 (:span
:class
"point-creator point-editor point-viewer"
1623 :id
"point-kind-select"
1624 :name
"point-kind-select"
1625 :class
"combobox-select write-permission-dependent"
1626 :onchange
(ps-inline
1627 (consolidate-combobox
1631 :id
"point-kind-input"
1632 :name
"point-kind-input"
1633 :class
"combobox-input write-permission-dependent"
1634 :onchange
(ps-inline
1635 (unselect-combobox-selection
1639 (:input
:id
"point-numeric-description"
1640 :class
"vanilla-input write-permission-dependent"
1642 :type
"text" :name
"point-numeric-description")
1645 :id
"point-description"
1648 :id
"point-description-select"
1649 :name
"point-description-select"
1650 :class
"combobox-select write-permission-dependent"
1651 :onchange
(ps-inline
1652 (consolidate-combobox
1653 "point-description"))
1656 :id
"point-description-input"
1657 :name
"point-description-input"
1658 :class
"combobox-input write-permission-dependent"
1659 :onchange
(ps-inline
1660 (unselect-combobox-selection
1661 "point-description"))
1664 (:button
:id
"delete-point-button" :disabled t
1666 :onclick
(ps-inline (delete-point))
1668 (:button
:disabled t
:id
"finish-point-button"
1671 (:div
:id
"uniquify-buttons"
1672 (:button
:id
"suggest-unique-button"
1675 (insert-unique-suggestion))
1677 (:button
:id
"force-duplicate-button"
1680 (:div
:id
"aux-point-distance-or-point-creation-date"
1681 (:code
:id
"point-creation-date"
1682 :class
"point-editor point-viewer")
1684 :id
"aux-point-distance" :disabled t
1685 :class
"point-creator aux-data-viewer aux-data-dependent"
1686 :size
1 :name
"aux-point-distance"
1687 :onchange
(ps-inline
1688 (aux-point-distance-selected))
1690 (enable-aux-point-selection)))
1692 :id
"include-aux-data"
1693 :class
"point-creator aux-data-dependent"
1695 (:input
:id
"include-aux-data-p"
1696 :class
"tight-input"
1697 :type
"checkbox" :checked t
1698 :name
"include-aux-data-p"
1699 :onchange
(ps-inline
1700 (flip-aux-data-inclusion)))
1702 (:div
:id
"display-nearest-aux-data"
1703 :class
"aux-data-viewer"
1705 (:input
:id
"display-nearest-aux-data-p"
1706 :class
"tight-input"
1707 :type
"checkbox" :checked t
1708 :name
"display-nearest-aux-data-p"
1709 :onchange
(ps-inline
1710 (flip-nearest-aux-data-display)))
1714 :class
"point-creator point-editor point-viewer aux-data-viewer"
1715 (:div
:id
"aux-numeric-list")
1716 (:div
:id
"aux-text-list")))
1717 (:div
:class
"walk-mode-controls"
1718 (:div
:id
"walk-mode"
1719 :class
"aux-data-dependent"
1720 (:input
:id
"walk-p"
1721 :class
"tight-input"
1722 :type
"checkbox" :checked nil
1723 :onchange
(ps-inline
1725 (:label
:for
"walk-p"
1727 (:div
:id
"decrease-step-size"
1728 :class
"aux-data-dependent"
1729 :onclick
(ps-inline (decrease-step-size)))
1730 (:div
:id
"step-size"
1731 :class
"aux-data-dependent"
1732 :onclick
(ps-inline (increase-step-size))
1734 (:div
:id
"increase-step-size"
1735 :class
"aux-data-dependent"
1736 :onclick
(ps-inline (increase-step-size))
1737 :ondblclick
(ps-inline (increase-step-size)
1738 (increase-step-size)))
1739 (:div
:id
"step-button" :disabled nil
1740 :class
"aux-data-dependent"
1741 :onclick
(ps-inline (step))
1742 :ondblclick
(ps-inline (step t
))
1744 (:div
:class
"image-main-controls"
1745 (:div
:id
"auto-zoom"
1746 (:input
:id
"zoom-to-point-p"
1747 :class
"tight-input"
1748 :type
"checkbox" :checked t
)
1749 (:label
:for
"zoom-to-point-p"
1751 (:div
:id
"brighten-images"
1752 (:input
:id
"brighten-images-p"
1753 :class
"tight-input"
1754 :type
"checkbox" :checked nil
)
1755 (:label
:for
"brighten-images-p"
1757 (:div
:id
"zoom-images-to-max-extent"
1758 :onclick
(ps-inline (zoom-images-to-max-extent)))
1759 (:div
:id
"no-footprints-p"
1761 (:div
:id
"remove-work-layers-button" :disabled t
1762 :onclick
(ps-inline (reset-layers-and-controls))
1764 ;; help area (northeast)
1768 :id
"download-user-points-button"
1770 :onclick
(format nil
1771 "self.location.href = \"/~A/lib/user-points.json\""
1773 "download points") ;TODO: offer other formats and maybe projections
1782 "/lib/blurb?openlayers-version="
1783 (@ *open-layers
*version_number
*))
1785 (:img
:src
(format nil
"/~A/lib/public_html/phoros-logo-plain.png"
1787 :alt
"Phoros" :style
"vertical-align:middle"
1789 (:button
:id
"logout-button"
1791 :onclick
(ps-inline (bye))
1793 (:h2
:id
"h2-help" "Help")
1794 (:div
:id
"help-display"))
1795 ;; image area (south)
1796 (:div
:id
"images" :style
"clear:both"
1798 for i from
0 below
*number-of-images
* do
1800 (:div
:class
"controlled-image"
1801 (:div
:id
(format nil
"image-~S-controls" i
)
1802 :class
"image-controls"
1803 (:div
:id
(format nil
"image-~S-zoom" i
)
1804 :class
"image-zoom")
1805 (:div
:id
(format nil
"image-~S-layer-switcher" i
)
1806 :class
"image-layer-switcher")
1807 (:div
:id
(format nil
"image-~S-usable" i
)
1808 :class
"image-usable"
1810 (:div
:id
(format nil
"image-~S-trigger-time" i
)
1811 :class
"image-trigger-time"))
1812 (:div
:id
(format nil
"image-~S" i
)
1813 :class
"image" :style
"cursor:crosshair"))))))))
1814 (hunchentoot:redirect
1815 (format nil
"/~A/~A"
1817 (hunchentoot:session-value
'presentation-project-name
))
1818 :add-session-id t
)))
1820 (hunchentoot:define-easy-handler
1821 (epipolar-line :uri
"/phoros/lib/epipolar-line")
1823 "Receive vector of two sets of picture parameters, the first of
1824 which containing coordinates (m, n) of a clicked point. Respond with a
1825 JSON encoded epipolar-line."
1826 (assert-authentication)
1827 (setf (hunchentoot:content-type
*) "application/json")
1828 (let* ((data (json:decode-json-from-string
(hunchentoot:raw-post-data
))))
1829 (json:encode-json-to-string
1830 (photogrammetry :epipolar-line
(first data
) (second data
)))))
1832 (hunchentoot:define-easy-handler
1833 (estimated-positions :uri
"/phoros/lib/estimated-positions")
1835 "Receive a two-part JSON vector comprising (1) a vector containing
1836 sets of picture-parameters with clicked (\"active\") points
1837 stored in :m, :n; and (2) a vector containing sets of
1838 picture-parameters; respond with a JSON encoded two-part vector
1839 comprising (1) a point in global coordinates; and (2) a vector of
1840 image coordinates (m, n) for the global point that correspond to the
1841 images from the received second vector. TODO: report error on bad
1842 data (ex: points too far apart)."
1843 ;; TODO: global-point-for-display should probably contain a proj string in order to make sense of the (cartesian) standard deviations.
1844 (assert-authentication)
1845 (setf (hunchentoot:content-type
*) "application/json")
1847 (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
1848 (active-point-photo-parameters
1850 (number-of-active-points
1851 (length active-point-photo-parameters
))
1852 (destination-photo-parameters
1855 (cdr (assoc :cartesian-system
1856 (first active-point-photo-parameters
))))
1857 (global-point-cartesian
1859 :multi-position-intersection active-point-photo-parameters
))
1860 (global-point-geographic-radians
1861 (proj:cs2cs
(list (cdr (assoc :x-global global-point-cartesian
))
1862 (cdr (assoc :y-global global-point-cartesian
))
1863 (cdr (assoc :z-global global-point-cartesian
)))
1864 :source-cs cartesian-system
))
1865 (global-point-for-display ;points geographic cs, degrees; std deviations in cartesian cs
1866 (pairlis '(:longitude
:latitude
:ellipsoid-height
1867 ;; :stdx-global :stdy-global :stdz-global
1870 (proj:radians-to-degrees
1871 (first global-point-geographic-radians
))
1872 (proj:radians-to-degrees
1873 (second global-point-geographic-radians
))
1874 (third global-point-geographic-radians
)
1875 ;; (cdr (assoc :stdx-global global-point-cartesian))
1876 ;; (cdr (assoc :stdy-global global-point-cartesian))
1877 ;; (cdr (assoc :stdz-global global-point-cartesian))
1878 number-of-active-points
)))
1881 for i in destination-photo-parameters
1884 (photogrammetry :reprojection i global-point-cartesian
)))))
1885 (json:encode-json-to-string
1886 (list global-point-for-display image-coordinates
))))
1888 (hunchentoot:define-easy-handler
1889 (user-point-positions :uri
"/phoros/lib/user-point-positions")
1891 "Receive a two-part JSON vector comprising
1892 - a vector of user-point-id's and
1893 - a vector containing sets of picture-parameters;
1894 respond with a JSON object comprising the elements
1895 - image-points, a vector whose elements
1896 - correspond to the elements of the picture-parameters vector
1898 - are GeoJSON feature collections containing one point (in picture
1899 coordinates) for each user-point-id received;
1900 - user-point-count, the number of user-points we tried to fetch
1902 (assert-authentication)
1903 (setf (hunchentoot:content-type
*) "application/json")
1904 (with-connection *postgresql-credentials
*
1905 (let* ((user-point-table-name
1906 (user-point-table-name (hunchentoot:session-value
1907 'presentation-project-name
)))
1908 (data (json:decode-json-from-string
(hunchentoot:raw-post-data
)))
1909 (user-point-ids (first data
))
1910 (user-point-count (length user-point-ids
))
1911 (destination-photo-parameters (second data
))
1913 (cdr (assoc :cartesian-system
1914 (first destination-photo-parameters
)))) ;TODO: in rare cases, coordinate systems of the images shown may differ
1918 (:as
(:st_x
'coordinates
) 'longitude
)
1919 (:as
(:st_y
'coordinates
) 'latitude
)
1920 (:as
(:st_z
'coordinates
) 'ellipsoid-height
)
1921 (:as
'user-point-id
'id
) ;becomes fid on client
1924 'numeric-description
1926 (:as
(:to-char
'creation-date
"IYYY-MM-DD HH24:MI:SS TZ")
1930 :from user-point-table-name
:natural
:left-join
'sys-user
1931 :where
(:in
'user-point-id
(:set user-point-ids
)))
1933 (global-points-cartesian
1935 for global-point-geographic in user-points
1937 (ignore-errors ;in case no destination-photo-parameters have been sent
1938 (pairlis '(:x-global
:y-global
:z-global
)
1941 (proj:degrees-to-radians
1942 (getf global-point-geographic
:longitude
))
1943 (proj:degrees-to-radians
1944 (getf global-point-geographic
:latitude
))
1945 (getf global-point-geographic
:ellipsoid-height
))
1946 :destination-cs cartesian-system
)))))
1949 for photo-parameter-set in destination-photo-parameters
1951 (encode-geojson-to-string
1953 for global-point-cartesian in global-points-cartesian
1954 for user-point in user-points
1956 (when (point-within-image-p
1957 (getf user-point
:id
)
1958 (hunchentoot:session-value
'presentation-project-name
)
1959 (cdr (assoc :byte-position photo-parameter-set
))
1960 (cdr (assoc :filename photo-parameter-set
))
1961 (cdr (assoc :measurement-id photo-parameter-set
)))
1963 (let ((photo-coordinates
1964 (photogrammetry :reprojection
1966 global-point-cartesian
))
1969 (setf (getf photo-point
:x
)
1970 (cdr (assoc :m photo-coordinates
)))
1971 (setf (getf photo-point
:y
)
1972 (cdr (assoc :n photo-coordinates
)))
1974 :junk-keys
'(:longitude
:latitude
:ellipsoid-height
)))))
1975 (with-output-to-string (s)
1976 (json:with-object
(s)
1977 (json:encode-object-member
:user-point-count user-point-count s
)
1978 (json:as-object-member
(:image-points s
)
1979 (json:with-array
(s)
1980 (loop for i in image-coordinates do
1981 (json:as-array-member
(s) (princ i s
))))))))))
1983 (defun point-within-image-p (user-point-id presentation-project-name
1984 byte-position filename measurement-id
)
1985 "Return t if either point with user-point-id is inside the footprint
1986 of the image described by byte-position, filename, and measurement-id;
1987 or if that image doesn't have a footprint. Return nil otherwise."
1988 (let* ((user-point-table-name (user-point-table-name
1989 presentation-project-name
))
1990 (presentation-project-id (presentation-project-id-from-name
1991 presentation-project-name
))
1992 (common-table-names (common-table-names presentation-project-id
)))
1997 for common-table-name in common-table-names
1998 for aggregate-view-name
1999 = (aggregate-view-name common-table-name
)
2003 :from
',aggregate-view-name
2004 :where
(:and
(:= 'byte-position
,byte-position
)
2005 (:= 'filename
,filename
)
2006 (:= 'measurement-id
,measurement-id
)
2007 (:or
(:is-null
'footprint
)
2009 (:select
'coordinates
2010 :from
,user-point-table-name
2011 :where
(:= 'user-point-id
2016 (hunchentoot:define-easy-handler
2017 (multi-position-intersection :uri
"/phoros/lib/intersection")
2019 "Receive vector of sets of picture parameters, respond with stuff."
2020 (assert-authentication)
2021 (setf (hunchentoot:content-type
*) "application/json")
2022 (let* ((data (json:decode-json-from-string
(hunchentoot:raw-post-data
))))
2023 (json:encode-json-to-string
2024 (photogrammetry :multi-position-intersection data
))))