Monday, April 29, 2013

java command-line input

import java.io.*;

public class ReadString {

   public static void main (String[] args) {

      //  prompt the user to enter their name
      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String userName = null;

      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         userName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your name!");
         System.exit(1);
      }

      System.out.println("Thanks for the name, " + userName);

   }

}  // end of ReadString class

Monday, April 22, 2013

Raster Data and Vector Data


Raster and vector are the two basic data structures for storing and manipulating images and graphics data on a computer. Major GIS and CAD (Computer Aided Design) software packages available today are primarily based on one of the two structures, either raster based or vector based, while they have some extended functions to support other data structures.

Raster images come in the form of individual pixels, and each spatial location or resolution element has a pixel associated where the pixel value indicates the attribute, such as color, elevation, or an ID number. Raster images are normally acquired by optical scanner, digital CCD camera and other raster imaging devices. Its spatial resolution is determined by the resolution of the acquisition device and the quality of the original data source. Because a raster image has to have pixels for all spatial locations, it is strictly limited by how big a spatial area it can represent. When increasing the spatial resolution by 2 times, the total size of a two-dimensional raster image will increase by 4 times because the number of pixels is doubled in both X and Y dimensions. The same is true when a larger area is to be covered when using same spatial resolution.

Vector data comes in the form of points and lines that are geometrically and mathematically associated. Points are stored using the coordinates, for example, a two-dimensional point is stored as (x, y). Lines are stored as a series of point pairs, where each pair represents a straight line segment, for example, (x1, y1) and (x2, y2) indicating a line from (x1, y1) to (x2, y2).

In general, vector data structure produces smaller file size than raster image because a raster image needs space for all pixels while only point coordinates are stored in vector representation. This is particularly true in the case when the graphics or images have large homogenous regions and the boundaries and shapes are the primary interest. Besides the size issue, vector data is easier than raster data to handle on a computer because it has fewer data items and it is more flexible to be adjusted for different scale, for example, a projection system in mapping application. This makes vector data structure the apparent choice for most mapping, GIS and CAD software packages. Also, topology among graphical objects or items are much easier to be represented using vector form, since a commonly shared edge can be easily defined according to its left and right side polygons. On the other hand, this is almost impossible or very difficult to do with pixels.

Friday, April 19, 2013

GIS material

http://workshops.opengeo.org/postgis-intro/#workshop-modules

http://workshops.opengeo.org/openlayers-intro/

http://workshops.opengeo.org/

http://workshops.opengeo.org/geoserver-intro/

http://docs.geoserver.org/stable/en/user/advanced/crshandling/index.html

http://docs.geoserver.org/stable/en/user/tutorials/cql/cql_tutorial.html

http://docs.geoserver.org/stable/en/user/services/wms/vendor.html#wms-vendor-parameters


http://docs.geoserver.org/stable/en/user/styling/sld-cookbook/index.html

http://suite.opengeo.org/geoexplorer/composer/

http://docs.geoserver.org/latest/en/user/data/vector/shapefile.html?highlight=shapefile
http://openlayers.org/dev/examples/sundials.html



http://openlayers.org/dev/examples/markers.html

http://docs.geoserver.org/2.1.4/user/data/wfs.html

http://fancybox.net/#inline2

http://jquerymobile.com/demos/1.2.0/docs/pages/popup/popup-iframes.html

http://www.jacklmoore.com/colorbox/

Persistence Links

https://jersey.java.net/nonav/documentation/latest/json.html


http://programmingobsession.blogspot.com/2010/01/jaxb-json.html

http://davidwalsh.name/convert-xml-json

http://api.jquery.com/jQuery.get/


how to parse html doc by JQuery search


Wednesday, April 3, 2013

Pushing Negation Inward


DeMorgan's Theorem

DeMorgan's Theorem has to do with negating conditions whose main operator are conditional AND and conditional OR. This is && and ||.Let's think about DeMorgan's Theorem. Suppose I say "I am going to buy you ice cream AND I am going to buy you cookies". When would I be lying? That is, when would the negation of what I say be true?
Suppose I just bought ice cream. Then I'd be lying, since I told you I'd also get cookies. Similarly, if I bought just cookies, then I didn't buy ice cream. Certainly, I'm lying if I don't buy you anything. Thus, I am lying if "I don't buy you ice cream OR I don't buy you cookies".
This OR is considered inclusive.
If I had said "I am going to buy you ice cream OR I am going to buy you cookies", I could also buy both.
Now let's consider the negation of the OR statement. When would I be lying?
Suppose I just bought ice cream. That's OK, because I said I'd buy one or the other. Suppose I bought cookies. That's fine too. Suppose I bought both ice cream and cookies. Well, you can't say I lied, right? What if I didn't buy either? Then, I'm lying.
So the negation of the OR statement is "I am not going to buy you ice cream AND I am not going to buy you cookies".
Both these equivalences make up DeMorgan's Theorem.
  • ! ( exprleft && exprright ) is equivalent to ! exprleft || ! exprright )
  • ! ( exprleft || exprright ) is equivalent to ! exprleft && ! exprright )
We didn't get rid of the !, but we did move it closer to the expression. If the expression contains a relational or equality operator, we can use the rules above to negate them. If they contain logical operators, we can apply DeMorgan's again. Eventually we get rid of the !.

Pushing Negation Inward

One way of looking at DeMorgan's Theorem is that it pushes negations inward. Initially, we had the ! operator applied to a large expression, then we pushed it to a smaller subexpression. We also flip AND to OR, and OR to AND.Now, DeMorgan's Theorem is an equality, which means that we can push negations outwards too (just do the reverse). However, mostly you push negations inward.

implementation of Multi Map



import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

/**
 * An implementation of a <code>MultiMap</code>, which is basically a Map
 * with multiple values for a key. This will be removed when SUN see sense and
 * include it in the JDK java.util package as standard.
 *
 * @version $Revision: 1.6 $
 */
public class MultiMap extends HashMap
{
    private transient Collection values=null;
   
    /**
     * Constructor.
     */
    public MultiMap()
    {
        super();
    }

    /**
     * Constructor.
     *
     * @param initialCapacity  the initial capacity
     */
    public MultiMap(int initialCapacity)
    {
        super(initialCapacity);
    }

    /**
     * Constructor.
     * @param initialCapacity initial capacity
     * @param loadFactor      load factor for the Map.
     */
    public MultiMap(int initialCapacity, float loadFactor)
    {
        super(initialCapacity, loadFactor);
    }

    /**
     * Constructor.
     * @param map  The initial Map.
     */
    public MultiMap(MultiMap map)
    {
        super();
        if( map != null )
        {
            Iterator it = map.entrySet().iterator();
            while( it.hasNext() )
            {
                Map.Entry entry = (Map.Entry) it.next();
                super.put(entry.getKey(), new ArrayList((List)entry.getValue()));
            }
        }
    }

    /**
     * Check if the map contains the passed value.
     *
     * @param value  the value to search for
     * @return true if the list contains the value
     */
    public boolean containsValue(Object value)
    {
        Set pairs = super.entrySet();

        if (pairs == null)
        {
            return false;
        }
        Iterator pairsIterator = pairs.iterator();
        while (pairsIterator.hasNext())
        {
            Map.Entry keyValuePair = (Map.Entry) pairsIterator.next();
            Collection coll = (Collection) keyValuePair.getValue();
            if (coll.contains(value))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * Add a key, and its value, to the map.
     *
     * @param key   the key to set
     * @param value the value to set the key to
     * @return the value added when successful, or null if an error
     */
    public Object put(Object key,Object value)
    {
        Collection c=(Collection)super.get(key);
        if (c == null)
        {
            c = createCollection(null);
            super.put(key, c);
        }
        boolean results = c.add(value);

        return (results ? value : null);
    }

    /**
     * Removes a specific value from map.
     * The item is removed from the collection mapped to the specified key.
     *
     * @param key  the key to remove from
     * @param item  the value to remove
     * @return the value removed (which was passed in)
     */
    public Object remove(Object key, Object item)
    {
        Collection valuesForKey=(Collection)super.get(key);
        if (valuesForKey == null)
        {
            return null;
        }
        valuesForKey.remove(item);

        // remove the list if it is now empty
        // (saves space, and allows equals to work)
        if (valuesForKey.isEmpty())
        {
            remove(key);
        }
        return item;
    }

    /**
     * Clear the map.
     */
    public void clear()
    {
        // Clear the mappings
        Set pairs=super.entrySet();
        Iterator pairsIterator = pairs.iterator();
        while (pairsIterator.hasNext())
        {
            Map.Entry keyValuePair=(Map.Entry) pairsIterator.next();
            Collection coll=(Collection)keyValuePair.getValue();
            coll.clear();
        }
        super.clear();
    }

    /**
     * Accessor for the values in the Map.
     * @return all of the values in the map
     */
    public Collection values()
    {
        Collection vs = values;
        return (vs != null ? vs : (values = new ValueElement()));
    }

    /**
     * Method to clone the Map. Performs a shallow copy of the entry set.
     * @return the cloned map
     */
    public Object clone()
    {
        MultiMap obj = (MultiMap) super.clone();

        // Clone the entry set.
        for (Iterator it = entrySet().iterator(); it.hasNext();)
        {
            Map.Entry entry = (Map.Entry) it.next();
            Collection coll = (Collection) entry.getValue();
            Collection newColl = createCollection(coll);
            entry.setValue(newColl);
        }
        return obj;
    }

    /**
     * Creates a new instance of the map value Collection container.
     *
     * @param c  the collection to copy
     * @return   new collection
     */
    protected Collection createCollection(Collection c)
    {
        if (c == null)
        {
            return new ArrayList();
        }
        else
        {
            return new ArrayList(c);
        }
    }

    /**
     * Representation of the values.
     */
    private class ValueElement extends AbstractCollection
    {
        public Iterator iterator()
        {
            return new ValueElementIter();
        }

        public int size()
        {
            int i=0;
            Iterator iter = iterator();
            while (iter.hasNext())
            {
                iter.next();
                i++;
            }
            return i;
        }

        public void clear()
        {
            MultiMap.this.clear();
        }
    }

    /**
     * Iterator for the values.
     */
    private class ValueElementIter implements Iterator
    {
        private Iterator backing;
        private Iterator temp;

        private ValueElementIter()
        {
            backing = MultiMap.super.values().iterator();
        }

        private boolean searchNextIterator()
        {
            while (temp == null ||
                   temp.hasNext() == false)
            {
                if (backing.hasNext() == false)
                {
                    return false;
                }
                temp = ((Collection) backing.next()).iterator();
            }
            return true;
        }

        public boolean hasNext()
        {
            return searchNextIterator();
        }

        public Object next()
        {
            if (searchNextIterator() == false)
            {
                throw new NoSuchElementException();
            }
            return temp.next();
        }

        public void remove()
        {
            if (temp == null)
            {
                throw new IllegalStateException();
            }
            temp.remove();
        }

    }
}