|
Thursday, 08 December 2011 09:27 |
@CollectionOfElements
@JoinTable(
name = "TRACE_PROPERTY",
joinColumns = { @JoinColumn(name = "TRACE_ID") },
uniqueConstraints = { @UniqueConstraint(columnNames = {"TRACE_ID", "NAME", "VALUE" }) })
@org.hibernate.annotations.MapKey(
columns = @Column(name = "NAME", nullable = false, length = 100),
type = @Type(
type = "org.hibernate.type.EnumType",
parameters = {
@Parameter(name = "enumClass", value = "x.y.z.ExternalPropertyName"),
@Parameter(name = "type", value = "12")}))
@Column(name = "VALUE", nullable = false, length = 255)
private Map properties = new HashMap();
 Read more: |
|
Wednesday, 24 August 2011 14:07 |
To flush the DNS cache, launch the command prompt (you might need to run it as administrator) and type the following command:
ipconfig /flushdns
 Read more: |
|
|
Sunday, 27 March 2011 00:52 |
// This is an attempt to take a shape file with multipolygons in it and convert it to one with linestrings.
//
//It doesn't work (which is why the shape file write is commented out).
//
//It does, however, display the new feature collection okay
//
// Libraries used come from
// http://www.vividsolutions.com/jts/jtshome.htm and
// http://docs.geotools.org/ (version 2.7)
/**
*
*/
package map.foremail;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import map.common.C;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.Transaction;
import org.geotools.data.collection.CollectionFeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.AttributeTypeBuilder;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.map.DefaultMapContext;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.LineSymbolizer;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;
import org.geotools.swing.JMapFrame;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.AttributeType;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiPolygon;
/**
* @author John
* Convert a multi-polygon shape file to a line-string shape file
*
* This displays the new collection okay, but blows up making the shape file
*/
public class TestConvertGeoTypes {
private static final boolean NEW = true; // If true, convert with new type. If false, create new collection, but use old type
private static final String TEMP_SHAPE_FILE = "tmp/TCGT.shp";
// Mainline
public static void main(String[] args) throws Exception {
// GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
// Coordinate[] coords = new Coordinate[] {
// new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6),
// new Coordinate(-2, 2), new Coordinate(-2, 0), new Coordinate(-8, 6),
// };
// LineString line = geometryFactory.createLineString(coords);
// Read shapefile composed of multipolygons
ShapefileDataStore oldStore = getDataStoreFromShapeFile(C.COUNTIES_IN);
SimpleFeatureSource oldSrc = oldStore.getFeatureSource();
SimpleFeatureCollection oldCol = oldSrc.getFeatures();
// Convert the polygons to linestrings
LineString[] lines = polys2lines(oldCol);
// Create new feature source for the line strings
SimpleFeatureType oldType = oldSrc.getSchema();
SimpleFeatureSource newSrc;
if ( NEW ) {
System.out.println("\n\n======= Making New One-String Source ======\n");
newSrc = makeNewSrc("New LineString", oldType, null, lines, "LineString", LineString.class);
} else {
System.out.println("\n\n======= Making Old Source ======\n");
Geometry[] geoms = getGeometriesFromFeatureColl(oldSrc.getFeatures());
newSrc = makeNewSrc("Rebuilt MultiPoly", oldType, null, geoms, "MultiPolygon", MultiPolygon.class);
}
// writeShapeFile(newSrc.getFeatures(), TEMP_SHAPE_FILE); // Commented out - fails
//
// Display on a map (this works)
//
DefaultMapContext map = new DefaultMapContext();
map.setTitle("Try Geo Conversion");
StyleBuilder sbldr = new StyleBuilder();
Style style = sbldr.createStyle();
LineSymbolizer liner = sbldr.createLineSymbolizer(Color.GREEN, 1);
// Rule rule = sbldr.createRule(line);
FeatureTypeStyle fts = sbldr.createFeatureTypeStyle(liner);
style.featureTypeStyles().add(fts);
map.addLayer(newSrc.getFeatures(), (Style)style);
JMapFrame.showMap(map);
}
//
// Convert mulipolygon features to array of linestrings
//
private static LineString[] polys2lines(SimpleFeatureCollection oldCol) {
SimpleFeatureIterator it = oldCol.features();
GeometryFactory fact = new GeometryFactory();
List al = new LinkedList();
while ( it.hasNext() ) {
SimpleFeature feature = it.next();
MultiPolygon mp = (MultiPolygon)feature.getAttribute(0);
int n = mp.getNumGeometries();
for ( int i = 0 ; i < n ; i++ ) {
Geometry g = mp.getGeometryN(i);
Coordinate[] coords = g.getCoordinates();
LineString ls = fact.createLineString(coords);
al.add(ls);
}
}
return al.toArray(new LineString[al.size()]);
}
//
// Make a new feature source, based on old type but convert to new type
//
private static SimpleFeatureSource makeNewSrc(String srcName, SimpleFeatureType oldType, SimpleFeatureType typeToUse, Geometry[] geoms, String geoTypeStr, Class> geoClass) throws IOException {
AttributeDescriptor[] ads = oldType.getAttributeDescriptors().toArray(new AttributeDescriptor[oldType.getAttributeCount()]);
AttributeDescriptor newAttDesc = makeAttributeDescriptor("the_geom", geoTypeStr, geoClass);
ads[0] = newAttDesc;
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
// b.addAll(ads);
b.addAll(ads );
b.setCRS( oldType.getCoordinateReferenceSystem());
b.setName( srcName );
b.setDefaultGeometry("the_geom");
SimpleFeatureType newFeatureType = b.buildFeatureType();
SimpleFeatureSource newSrc = makeFeatureSourceGivenReference(Arrays.asList(geoms), typeToUse == null ? newFeatureType : typeToUse);
return newSrc;
}
//
// Make data store based on a shape file
public static ShapefileDataStore getDataStoreFromShapeFile(String fnameIn) throws Exception {
FileDataStore fds = FileDataStoreFinder.getDataStore(new File(fnameIn));
ShapefileDataStore shpStore = (ShapefileDataStore)fds;
return shpStore;
}
//
// Get all geometries from the feature collection (assume it is a multipolygon type)
public static Geometry[] getGeometriesFromFeatureColl(SimpleFeatureCollection col) {
List geomList = new ArrayList();
SimpleFeatureIterator it = col.features();
while ( it.hasNext() ) {
SimpleFeature feature = it.next();
// Note assuming here that we have a polygon shapefile
Geometry dfltGeom = (Geometry)feature.getDefaultGeometry();
if ( dfltGeom instanceof MultiPolygon ) {
MultiPolygon mp = (MultiPolygon) feature.getDefaultGeometry();
final int n = mp.getNumGeometries();
for (int i = 0; i < n; i++) {
geomList.add(mp.getGeometryN(i));
}
} else if ( dfltGeom instanceof LineString ) {
geomList.add((LineString)dfltGeom.getGeometryN(0));
}
}
return geomList.toArray(new Geometry[geomList.size()]);
}
//
// Write a shape file based from a feature collection
public static void writeShapeFile(SimpleFeatureCollection col, String name) throws Exception {
File newFile = new File(name);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map params = new HashMap();
params.put("url", newFile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
SimpleFeatureType schema = col.getSchema();
schema.getGeometryDescriptor();
newDataStore.createSchema(schema);
newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(col);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
System.exit(0); // success!
} else {
System.out.println(typeName + " does not support read/write access");
System.exit(1);
}
}
//
// Make an attribute descriptor
public static AttributeDescriptor makeAttributeDescriptor(String descName, String typeName, Class> clazz) {
AttributeTypeBuilder tbld = new AttributeTypeBuilder();
//set type information
tbld.setName( typeName );
tbld.setBinding( clazz );
tbld.setNillable( false );
//build the type
AttributeType atype = tbld.buildType();
AttributeTypeBuilder builder = new AttributeTypeBuilder();
//set type information
builder.setBinding( LineString.class );
builder.setNillable( false );
//set descriptor information
builder.setMinOccurs(0);
builder.setMaxOccurs(1);
builder.setNillable(true);
//build the descriptor
AttributeDescriptor descriptor = builder.buildDescriptor(descName, atype);
return descriptor;
}
//
// Make a feature source given a reference feature type
public static SimpleFeatureSource makeFeatureSourceGivenReference(Collection geometries, SimpleFeatureType refType) {
int i = 0;
SimpleFeatureCollection newCollection = FeatureCollections.newCollection();
for ( Geometry g : geometries ) {
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(refType);
//add the attributes
builder.add( g );
builder.add( "the_geom" );
//build the feature
SimpleFeature feature = builder.buildFeature( ""+(i++) );
newCollection.add(feature);
}
CollectionFeatureSource src = new CollectionFeatureSource(newCollection);
return src;
}
}
 Read more: |
|