Parsing KML in Java using JavaApiForKml.jar

KML(Keyhole markup language)

KML(Keyhole markup language) is a file format used to display geographic data in an Earth browser such as Google Earth, Google Maps, and Google Maps for mobile. KML uses a tag-based structure with nested elements and attributes and is based on the XML standard. All tags are case-sensitive and must be appear exactly as they are listed in the KML Reference. The Reference indicates which tags are optional. Within a given element, tags must appear in the order shown in the Reference.(From google doc: https://developers.google.com/kml/documentation/kml_tut)

Sample kml file:

<?xml version="1.0"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>Images from 2012-04-19T03:00:00Z UTC to 2012-04-19T03:00:00Z UTC</name>
<open>1</open>
<Folder>
<name>Channel 1</name>
<GroundOverlay>
<LookAt>
<heading>0</heading>
<latitude>10.00</latitude>
<longitude>96.00</longitude>
<tilt>0</tilt>
</LookAt>
<name>2012-04-19T03:00:00Z</name>
<visibility>0</visibility>
<Icon>
<href>http://tt.jpg</href>
</Icon>
<LatLonBox>
<north>25</north>
<south>-5</south>
<east>116</east>
<west>76</west>
</LatLonBox>
<TimeSpan>
<begin>2012-04-19T02:00:00Z</begin>
<end>2012-04-19T03:00:00Z</end>
</TimeSpan>
</GroundOverlay>
</Folder>
</Document>

Parsing KML using JavaApiForKml:
1. Download JavaApiForKml.jar from: http://code.google.com/p/javaapiforkml/
2. Add JavaApiForKml.jar in your java project
3. Check out following code to parsing KML

package com.kml;

import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.Feature;
import de.micromata.opengis.kml.v_2_2_0.Folder;
import de.micromata.opengis.kml.v_2_2_0.GroundOverlay;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.LatLonBox;
import de.micromata.opengis.kml.v_2_2_0.LookAt;
import java.io.File;
import java.util.List;

/**
 *
 * @author Shaiful Isalam(palash)
 */
public class KMLTest {

    public static void main(String[] args) {
        System.out.println("This is KML test");
        final Kml kml = Kml.unmarshal(new File("F:\\Custom.kml"));
        final Document document = (Document)kml.getFeature();
        System.out.println(document.getName());
        List<Feature> t = document.getFeature();
        for(Object o : t){
            Folder f = (Folder)o;
            List<Feature> tg = f.getFeature();
            for(Object ftg : tg){
                GroundOverlay g = (GroundOverlay) ftg;
                LatLonBox l = g.getLatLonBox();
                System.out.println(l.getNorth() );
                LookAt lk = (LookAt)g.getAbstractView();
                System.out.println(lk.getLatitude());                
            }
        }
        

    }
}
Advertisements

Registry 32-bit dll file in 64-bit OS(windows7)

To registry dll file into windows 7, at first you have to open command prompt(cmd) with administrative power. To gain administrative power please follow the screen shot

Registry 32-bit dll file in 32-bit OS/Registry dll file in OS:
  • Go to c:\windows\system32 folder using Command prompt.
  • Write regsvr32 yourdll.dll

Registry 32-bit dll file in 64-bit OS:
  • Go to c:\windows\SysWOW64 folder using Command prompt.
  • Write regsvr32 yourdll.dll

 

How to override hashCode() in ideal way #Java

Following steps can be followed to override java hashCode() in ideal way

1. Store some constant nonzero value, say 17, in an int variable called result

2. For each significant field f in your object (each field taken into account by the equals method, that is), do the following:

a. Compute an int hash code c for the field:

i) If the field is a boolean, compute (f ? 0 : 1)
ii) If the field is a byte, char, short, or int, compute (int)f.
iii) If the field is long, compute (int)(f^(f>>>32)).
iv) If the field is a float compute Float.floatToIntBits(f).
v) If the field is a double, compute Double.doubleToLongBits(f), and then hash the resulting long as in step 2.a.iii.
vi) If the field is an object reference and this class’s equals method compares the field by recursively invoking equals, recursively invoke hashCode on the field. If a more complex comparison is required, compute a “canonical representation” for this field and invoke hashCode on the canonical representation. If the value of the field is null, return 0.
vii) If the field is an array, treat it as if each element were a separate field. That is, compute a hash code for each significant element by applying these rules recursively, and combine these values as described in step 2.b.

b. Combine the hash code c computed in step a into result as follows: result = 37 * result + c;

3. Return result

4. When you done writing the hashCode method, ask yourself whether equal instances have equal hash codes. If not, figure out why and fix the problem.

An example of hashCode():

    @Override
    public int hashCode(){
        int result = 17 + Float.floatToIntBits(re);
        result = 37 * result + Float.floatToIntBits(im);
        return  result;
    }