I wrote a small java class to extract country boundaries from naturalearthdata.com data. My problem is that the coordinates do not match with OSM map, the whole polygon is shifted... e.g. according to the data extracted, the most southern point of Czech Rep. is
48.557915752000014,14.31574589000013
and here it is displayed on openstreetmap, about 1km far away from what I expect:
If I understand well, OSM uses wgs84, naturalearth uses wgs84 so I don't need to do any transform ? Is there something that I am missing?
Here is the class:
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.GeometryAttributeImpl;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeocentricCRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.Feature;
import org.opengis.feature.Property;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
public class Converter {
static Logger logger = Logger.getLogger(Converter.class);
static void extract(Feature feature,String iso2) throws Exception{
logger.info("extract "+iso2);
GeometryAttributeImpl geometry=(GeometryAttributeImpl)feature.getProperty("the_geom");
MultiPolygon multiPolygon=(MultiPolygon)geometry.getValue();
logger.info("polygons:"+multiPolygon.getNumGeometries());
for(int i=0;i<multiPolygon.getNumGeometries();i++){
logger.info("---------------------------");
logger.info(iso2+"-"+i);
Polygon polygon=(Polygon) multiPolygon.getGeometryN(i);
LineString lineString=polygon.getExteriorRing();
logger.info("points: "+lineString.getCoordinates().length);
System.out.println("points for "+iso2+"-"+i);
Coordinate min=lineString.getCoordinates()[0];
for(Coordinate coordinate:lineString.getCoordinates()){
System.out.println(coordinate.y+","+coordinate.x);
if (coordinate.y<min.y)
min=coordinate;
}
System.out.println("min="+min.y+","+min.x);
}
}
static void extract(String shapefile,String nameAttr,String nameMatch) throws Exception{
ShapefileDataStore ds = new ShapefileDataStore(new URL("file://"+shapefile));
FeatureSource fs = ds.getFeatureSource();
FeatureCollection fc = fs.getFeatures();
logger.info("features found:"+fc.size());
Iterator iterator = fc.iterator();
while (iterator.hasNext()) {
// Extract feature and geometry
Feature feature = (Feature) iterator.next();
Property propertyName=feature.getProperty(nameAttr);
if (propertyName==null){
logger.error("property "+nameAttr+" not found. valid properties are:");
for(Property property:feature.getProperties()){
logger.info(" "+property.getName());
}
}
showProperty(feature,"NAME");
showProperty(feature,"ISO_A3");
String name=((String)feature.getProperty(nameAttr).getValue());
logger.info("found "+nameAttr+"="+name);
if (nameMatch.equalsIgnoreCase(name) || name==null)
extract(feature,name.toLowerCase());
}
}
static void showProperty(Feature feature,String what) {
Property propertyNameName=feature.getProperty(what);
if (propertyNameName!=null)
logger.info(what+":"+propertyNameName.getValue());
else
logger.info(what+" not set");
}
public static void main(String ... args) throws Exception{
logger.info("start");
extract("/material/naturalearthdata/ne_10m_admin_0_countries/ne_10m_admin_0_countries.shp",
"ISO_A3","cze");
}