Friday 28 May 2010

Solution to Joe Celko's SQL puzzles & answers‎ - PUZZLE 9 AVAILABLE SEATS, Page 34

The Problem : You have a restaurant with 1,000 seats. Whenever a waiter puts someone at a seat, he logs it in a table of seats. Likewise, when a guest finishes a meal, you remove the guest’s seat number. You want to write a query to produce a list of the available seats in the restaurant, set up in blocks by their starting and ending seat numbers. Oh yes, the gimmick is that the database resides on a personal digital assistant and not a mainframe computer. As part of the exercise, you must do this with the smallest amount of storage possible. Assume each seat number is an integer. (extract from Joe Celko's SQL puzzles & answers‎ - PUZZLE 9 AVAILABLE SEATS, Page 34).

My set based solution with no procedural constructs like while loops or cursors)  goes like this:

/* Start by creating a new blank database, will name it [Puzzle] */
USE [Puzzle]

/* creating the table, will occupy 1000 char X 1 byte each = 1000 bytes */
   CREATE
     TABLE dbo.Seats
                  ( Seats char(1000) )

/* then insert all seats as empty */
INSERT INTO dbo.Seats VALUES ( REPLICATE('0',1000))

/* Then we need a way of set each seat as free/occupied */
CREATE PROCEDURE [dbo].[Set_Seat]
   @seatNum smallint,
   @occupied bit
AS
BEGIN
SET NOCOUNT ON;
  
/* This statetement will split the char string in two parts, setting the middle with the occupied value, concat them all together again, taking care of any weird case as well. */
UPDATE dbo.Seats
     SET Seats = CASE
                            WHEN @seatNum BETWEEN 0 AND 1000
                                THEN LEFT(Seats,@seatNum-1)
                                  + CAST(@occupied as char(1))
                                  + RIGHT(Seats,(LEN(Seats)
                                  - @seatNum))
                            ELSE Seats
                        END
END
/* then use this stored procedure to update the table, i.e. the following statement will update the the seats no 1,5,10 as occupied. */
EXECUTE [Puzzle].[dbo].[Set_Seat] @seatNum = 1, @occupied = 1
EXECUTE [Puzzle].[dbo].[Set_Seat] @seatNum = 5, @occupied = 1
EXECUTE [Puzzle].[dbo].[Set_Seat] @seatNum = 10, @occupied = 1

/* after executing this the table will look like this */
Seats
10001000010000000....

/* then we will need to have a way to see which seats are free and which are occupied.
To achieve this will need a helper number table, which is obtained by cross join the sys.objects table of the   [Puzzle] db. Because the db doesn't contain many user created objects, will double cross join to it self so as to achieve a cartesian product. This will give us at least a 1000 rows containing system information. By applying the funtion ROW_NUMBER() we can get the amount of numbered rows we want. */

/* the free seats */
CREATE VIEW [dbo].[vFreeSeats]
AS
SELECT a.Seat
  FROM ( SELECT n as Seat
                   FROM ( SELECT row_number() over (order by n.object_id) as ID
                                    FROM sys.objects n
                                       CROSS JOIN sys.objects v
                                   ) D ( n )
               WHERE n <= 1000
               ) a
 WHERE SUBSTRING((select * from dbo.Seats),a.Seat,1) = '0'
GO

/* and the occupied */
CREATE VIEW [dbo].[vOccupiedSeats]
AS
SELECT a.Seat
  FROM ( SELECT n as Seat
                   FROM ( SELECT row_number() over (order by n.object_id ) as ID
                                    FROM sys.objects n
                                        CROSS JOIN sys.objects v
                                 ) D ( n )
                WHERE n <= 1000
               ) a
 WHERE SUBSTRING((select * from dbo.Seats),a.Seat,1) = '1'
GO

/* Obtaining a plan of the restaurant using the same patern with row_number() to get a numbering from 1 to 1000, and then LEFT JOIN to dbo.vOccupiedSeats and dbo.vFreeSeats */
CREATE VIEW [dbo].[vAllSeats]
AS
SELECT s.Seat,
                o.Seat as Occupied,
                f.Seat as Free
       FROM ( SELECT n as Seat
                        FROM
                         ( SELECT row_number() over (order by n.object_id) as ID
                             FROM sys.objects n
                                CROSS JOIN sys.objects v
                          ) D ( n )
      WHERE n <= 1000 ) s

       LEFT JOIN dbo.vOccupiedSeats o
          ON o.Seat = s.Seat
           LEFT JOIN dbo.vFreeSeats f
               ON f.Seat = s.Seat
GO

/* Showing the start and end number of continous availabel blocks of seats, taking care for special cases as well. */
CREATE VIEW [dbo].[vFirstLastSeat]
AS
SELECT  f.[First] as FirstInBlock,
                l.[Last] as LastInBlock
  FROM (  /* get the first seat number of
                    each available continous block of seats */
               SELECT row_number() over (order by current_timestamp) as ID,
                             [First]
                   FROM ( /*  use this in case the first free seat is no 1 */
                                 SELECT 1 as [First]
                                   FROM dbo.vFreeSeats f
                                 WHERE f.Seat = 1
                                  UNION ALL
                                 /* this will return the first seats on a  
                                    continuous block,  except when
                                     this is seat no1*/
                                 SELECT f.Seat as [First]
                                   FROM dbo.vFreeSeats f
                                     JOIN dbo.vOccupiedSeats o
                                        ON f.Seat = o.Seat + 1 ) f
                      ) f
     
     /* get the last seat number of each available continous block of seats */
     JOIN ( SELECT row_number() over ( order by  current_timestamp ) as ID,
                                l.Seat as [Last]
                 FROM
                   ( /* this will return the first seats on a continuus 
                         block, except when this is seat no 1000 */
                      SELECT f.Seat
                        FROM dbo.vFreeSeats f
                            JOIN dbo.vOccupiedSeats o
                               ON f.Seat = o.Seat - 1
                       UNION ALL                                                
                       /* use this in case the last free seat is no 1000 */
                       SELECT 1000 as [Last]
                          FROM dbo.vFreeSeats
                        WHERE Seat = 1000 ) l
                  ) l

           ON f.ID = l.ID

/* The output of the previous statement */
FirstInBlockLastInBlock
24
69
111000


/* Finally, if we didn't want to use the sys.objects table, we could have create a @tempTable with 10 null rows of bit datatype, and use this to cross join 3 times to give a helper number table of a 1000 rows. */

Thursday 20 May 2010

Parallel Environments within Divine and Agnostic Algorithms of Creation

Extract from:
Sentient Future Competition European Workshop on Sensor Networks 2006 (EWSN '06)  - Highly commended entries :
http://www.embedded-wisents.org/competition/pdf/bairaktaris.pdf

"Model and create a new environment using the Earth libraries we provide - minus the historical and contemporary ethical and philosophical classes. With the use of evolution algorithms, and by fast forwarding them for time and space complexity aspects, simulate genetical transformations, aiming the creation of intelligence within the system. You are expected to experiment with the parameters until the creation of viable and self contained entities (who will have the power to interact intellectually between themselves and their environment) are created. From the moment of creation and afterwards you should observe the behaviour of those entities but you are not allowed to interfere.

Your objective is to be God, and you’ll achieve this if the entities created develop analytical and philosophical qualities in the amount that they will start wondering from where and for what they’ re created for. Full marks will be granted if a form of technological advance is achieved by the entities. If this happens, as a bonus you are allowed to include in your project the full version of Embedded WiSeNts libraries – such this will ease the way of your entities to upgrade themselves the best way is possible in their future..."

Sunday 16 May 2010

SQL POETRY : to @exist or not @exist - that is the query

start:
BEGIN TRAN
DECLARE @var INT, @exist VARCHAR
SET @var = 0

INSERT
    INTO Who
SELECT [to], [thine], [own], [self], [be], [true]
  FROM Life
      JOIN Earth
        ON [lifetime] BETWEEN [birth] AND [death]
 WHERE [time] = 'Now'
     AND [name] = 'You'

/* then */
IF (SELECT COUNT(*) FROM Who) > @var
BEGIN
     SET @exist = dbo.YourOwnFunction(@@IDENTITY)
END
ELSE
BEGIN
    SET @exist = null
    ROLLBACK TRANSACTION
    GOTO start
END

COMMIT

Thursday 6 May 2010

Practical C - Mobile GIS

C.1 Limitations of this tool
ArcMap is able to perform analytical tasks on large volumes of data, but when compared to the latest developments in the mobile GI field using it to create a mobile directory seems like overkill.

Mobility is restricted as it operates on static datasets whilst requiring a client with significant processing capacity and an external server offering interaction and presentation GI services.

Using reverse-geocoding the tool is able to perform spatial queries depending on post-code information, however with each post-code indentifying between 1 and 100 unique addresses, the accuracy level of the spatial query performed is depended on the type of location clicked on the map (street numbers on larger roads usually have longer distance between them). In addition, the search tolerance setting on the ArcMap document, used within the VB script1 will influence heavily the post-code returned.

C.2 Is Google Local thorough in identifying available services? How about searching information other than 'services'; What type of limitations exist in querying spatial information in generally?

 Google Local is a location-based service, accessible via thin client architecture. It allows querying by specifying various spatial search criteria and is supported by services2 responsible to convert the textual descriptions to spatial coordinates. However, with queries containing evaluative criteria such as ‘which is the best restaurants in Leytonstone’, the results span all over across London; the user needs to pan the map or filter based on distance to focus in the area preferred. When the term ‘best’, was replaced with ‘cheapest’, irrelevant results regarding property maintenance services were returned. More ambiguous qualitative queries like ‘the most expensive street in London’ did not return relevant results. It is understood that such a complex process of extracting semantically correct meaning out of verbose queries spans in many different scientific research fields, including linguistics, AI etc, therefore such inconsistencies in the results must be within the user’s expectations. But is apparent that the work that has been invested in such a tool has yield in a extremely efficient service with lots of options, which it can only be further improved.

C.3 Would a search of all Internet resources provide further information; What alternatives are there to using a postcode for spatial queries;

Extending the search using a search engine yields many results with potentially exact and relevant information; however there are fundamental differences in the algorithms behind search engines with those behind GI directory services which suffer from the lack of universally agreed geography type; An attempt to tackle this problem is provided by the GeoXwalk3 project: geometric computations introduce a mechanism to resolve spatially complex queries4 describing relationships between features with complex geographic boundaries. Further development has been done to enable advanced querying5 by “explicitly georeference implicitly georeferenced material” (geoXwalk Phase III - Final Report, 2004) .


C.4 Availability in other regions and countries

According to Google(6), there is an ongoing process on making this service available everywhere in the world, but this is not the case yet; though a workaround exists by creating a public My Map to list any business information which then can be searched using Google Maps7.


C.5 References

1. ‘ get the postcode
      Dim postcode As String
      postcode = getPostcode(pMxDoc.SearchTolerance, pPoint, pMxDoc.FocusMap)
2. Such as gateway location services, directory services, geocode services, route determination services etc.
3. Reid, James & Medyckyj-Scott, David., 2004. GeoXwalk Phase III – Final Report. Available at: http://edina.ac.uk/projects/geoxwalk/documents/geoXwalk%20Phase%20III%20Final%20report.doc[Accessed April 12, 2010].
4. i.e. ‘which rivers are near Banbury? ’ (geoXwalk Phase III – Final Report, 2004)
5. i.e. ‘find me all documents about Gaelic songs that do not reference the Western Isles or, find me images of towns along the river Tweed’ (geoXwalk Phase III – Final Report, 2004)
6. Maps features available in your country – Maps Help. Available at: http://maps.google.com/support/bin/answer.py?hl=en&answer=16634 [Accessed April 12, 2010].
7. http://www.google.com/support/forum/p/maps/thread?tid=7f1a2dcf8c70f97c&hl=en

Practical B - Internet GIS

B.1 Functionality, strengths and weaknesses

A core element of Web 2.0, mashups enable information sharing and interoperability by combining data from different external sources. With the adoption of web standards like RSS, online tools now allow the user to create and publish mash-ups with relative ease. Such tools are the Mapbuilder (tool for overlaying pin-points and tags over a map) and the Yahoo Pipes, with more sophisticated integration of various data sources. Both use cartographic and geographic data to produce a map; the user can then choose to add information related to location.

The produced examples have as intended audience the consumer. The Mapbuilder shows the locations of local sites in the island of Ikaria, Greece. The audience of Yahoo Pipes mashup could be someone interest in the world news with an option of accessing location related photos; this is achieved by overlaying the Reuters News RSS feed on a map using each story's location information, plus combining it with the Flicker Geo RSS service to produce a link on a relevant to location photo. There is a strong chance that the latest geo-tagged photo with a location same as the story will have relevant content though there is no guarantee of this.

B.2 Yahoo pipes usability and mashup implementation(4)

Yahoo Pipes is a Javascript application with a visual interface that includes various tools referred to as modules. Each module can accept a data source as input, manipulate it, and pipe its output to a different module. The process follows the imperative programming paradigm: inputs and outputs can be processed using modules like the Loop operator (iterating over the result-feed of its input), String functions, URLBuilder etc. The final feed can be georeferenced by using the Location Extractor module; given a string that contains a place name it attaches GeoRSS standard spatial coordinates to it. Inherent ambiguity limits its accuracy; different places with the same name do exist. The output stream of data can be exported as JSON or RSS feed. Of particular interest, is the YQL module: using sql-like syntax the user can query and filter the content of an RSS feed. This feature is used on the example pipe. Finally, the RSS-Item Builder restructures the modified data sources as an RSS feed, which then can be piped to the output.

B.3 Usability, flexibility and extensibility

Both engines are easy to use for simple mashups, though Yahoo Pipes extended capabilities can challenge users with little or no programming experience. Additionally, Yahoo Pipes user interface is computationally demanding; integrating data from different web-services in the client machine proved a difficult task without reliable and fast internet connection, with errors related to connection refusal. According to Yahoo, there is an ongoing redesigning of the data flow engine based on the YQL functionality(3). Finally, Yahoo Pipes Badge allows the map output to be embedded in any web page by providing a widget with a guid; an excellent example of how GI mashups can add value to a website free of cost.




B.4 References

 1. Map Builder url: http://www.mapbuilder.net/users/panoramix95/83622
 2. Yahoo Pipes url: http://pipes.yahoo.com/gita_abhp626/reuter_news_and_flicker_photos
 3. Pipes Blog » Blog Archive » Connection refused and other Pipes issues. Available at: http://blog.pipes.yahoo.net/2009/12/10/connection-refused-and-other-pipes-issues/
[Accessed April 7, 2010]
 4. Pipe implementation screen shot

Practical A - Geodesy and positioning

A.1 The shape and the route

Regent’s Park’s inner circle made a good start point to explore routes appropriate for track-logging with some kind of pattern. The route was planned using Google maps service; its vector based maps are simple without excessive visual variables creating clutter; thus facilitating pattern recognition. The produced trail when projected on the map can be seen as a stickman with arms extending left and right in a throwing motion and legs positioned in a manner that indicates throw-assisting movement. With a relative quantum leap of imagination there is a scarf tied around the neck as well. In hindsight, producing this pattern was an ambitious aim; the track was traced on foot, and took a considerable amount of time. Non-anticipated closed paths encountered around the ‘bucket’ area and are responsible for the ‘scarf’ detail. In a perfect world, the long neck would have been much shorter and the circle-head could contain additional detail within.


A.2 Accuracy, error and appropriateness of the map

The GPS track was collected uninterrupted during one afternoon; during data collection the sky was clear with good visibility, and the GPS was triangulating consistently using 4 satellites. The only time that the signal was lost, was due to street narrowness and existence of big trees obscuring the sky (Park Crescent Mews str. with Marylebone Rd). In this case the GPS lost visibility of all satellites, but the track path was not affected as the distance covered under such conditions was very short.

Is fairly obvious that the overall match of the GPS data and the backdrop map is quite good, making apparent that there was a sufficiently strong signal, tracking the route with high accuracy. There aren’t any particular mismatches, and where a multipath is plotted is due to the paths followed to draw the stickman’s hands and legs which were backtracked – thus track-logged twice. Occasionally, people and traffic lights necessitated diversion from a straight route.

The GPS track was collected according to the WGS841 geodetic system; to project on a 2D coordinate system, the track was transferred to ArcView via the GPSi interface and transformed using the OS British Grid2 by selecting the OSGB_1936_To_WGS_1984_Petroleum3 transformation, which allows for a deviation(4) up to 4 metres; thus there is certainly some degree of error since no exact transformation between two geodetic coordinate systems exist(5). During the transformation process a message ‘The output resolution is larger than the input feature class resolution’ was produced, due to the fact that the output geoprocessing tools in ArcGIS 9.2 is 53-bit7.

Finally, to overlay the track, two backdrop maps, one raster and one vector based were used. The scale of the raster map is 1:25,000, allowing the streets to be distinctly visible; both maps were downloaded from Edina. The track log, and the two maps where overlaid using ArcMap. Note that it was not necessary to create a new Geodatabase, and all layers where created by simple opening the Mastermap gz file.
 


A.3 References

1. A 3D geodetic coordinate system, that comprises a standard coordinate frame for the earth, with a spheroid surface. (http://en.wikipedia.org/wiki/World_Geodetic_System)
2. A system of geographic grid references commonly used in Great Britain.
http://en.wikipedia.org/wiki/British_national_grid_reference_system
3. This transformation uses the parameters recommended by Ordnance Survey for a Helmer(6) transformation; http://geometrybag.wordpress.com/2006/05/03/osgb-transformations-inside-arcgis/
4. A statistic value that represents the amount of disagreement among points with known longitude and latitude in a coordinate system, when compared with a different coordinate system. http://www.ordnancesurvey.co.uk/oswebsite/gps/information/coordinatesystemsinfo/guidecontents/guide6.html
5. Welcome to GPS Network. Ordnance Survey – Great Britain's national mapping agency. www.ordnancesurvey.co.uk/oswebsite/gps/information/coordinatesystemsinfo/guidecontents/guide6.html
[Accessed March 31, 2010].
6. Helmert transformation: The rotation and translation of a network of points relative to the Cartesian axes, while leaving the shape of the figure unchanged;
7. Technical Articles - ESRI Support. Available at: http://support.esri.com/index.