Hi,
I have the same problem. I would like to receive the same example you sent
Adrian.
That is because i receive the error message below, too.
Exception in thread "AWT-EventQueue-0"
org.geotools.factory.FactoryNotFoundException: No factory of kind
"GeometryFactory" found.
at
org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:374)
at
org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:143)
at
org.geotools.geometry.jts.JTSFactoryFinder.getGeometryFactory(JTSFactoryFinder.java:87)
See as follows, my code:
/*
* frmPrincipal.java
*
* Created on 22 de Outubro de 2008, 15:01
*/
package mapprs;
//import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.gui.swing.About;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
/**
*
* @author User
*/
public class frmPrincipal extends javax.swing.JFrame {
/** Creates new form frmPrincipal */
public frmPrincipal() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Ler Arquivo Shape");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Ler Arquivo Shape - 2");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("jButton3");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(150, 150, 150)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE, 216,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 216,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(158, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(431, Short.MAX_VALUE)
.addComponent(jButton3)
.addGap(20, 20, 20))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jButton1)
.addGap(38, 38, 38)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 182,
Short.MAX_VALUE)
.addComponent(jButton3)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
try {
//File f = new File("C:\\ShapesTeste\\T_Poligono_UTM.shp");
//File f = new File("C:\\ShapesTeste\\EstadoACRE.shp");
File f = new File("C:\\ShapesTeste\\BR_MUNICIPIO_IBGE_2006.SHP");
Map<String, Serializable> connectParameters = new HashMap<String,
Serializable>();
connectParameters.put("url", f.toURI().toURL());
connectParameters.put("Cria Index Spacial", true);
try {
DataStore dataStore =
DataStoreFinder.getDataStore(connectParameters);
//SimpleFeatureType sft = dataStore.getSchema("T_Poligono_UTM");
//SimpleFeatureType sft = dataStore.getSchema("EstadoACRE");
SimpleFeatureType sft =
dataStore.getSchema("BR_MUNICIPIO_IBGE_2006");
Object[] obj = sft.getAttributeDescriptors().toArray();
// we are now connected
String[] typeNames = dataStore.getTypeNames();
String typeName = typeNames[0];
System.out.println("Reading content " + typeName);
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
FeatureIterator<SimpleFeature> iterator;
featureSource = dataStore.getFeatureSource(typeName);
collection = featureSource.getFeatures();
iterator = collection.features();
double totalLength = 0.0;
try {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
Geometry geometry = (Geometry)
feature.getDefaultGeometry();
geometry.getBoundaryDimension();
geometry.getCentroid();
geometry.getArea();
geometry.toString();
Coordinate[] coord = geometry.getCoordinates();
feature.getAttributeCount();
Object[] obj2 = feature.getAttributes().toArray();
if (obj2[0] instanceof MultiPolygon) {
MultiPolygon mp = (MultiPolygon) obj2[0];
mp.getGeometryType();
mp.getDimension();
mp.getBoundaryDimension();
} else if (obj2[0] instanceof MultiLineString) {
MultiLineString mls = (MultiLineString) obj2[0];
mls.getGeometryType();
mls.getDimension();
mls.getBoundaryDimension();
} else if (obj2[0] instanceof Point) {
Point p = (Point) obj2[0];
p.getGeometryType();
p.getGeometryType();
p.getDimension();
p.getBoundaryDimension();
}
totalLength += geometry.getLength();
System.out.println();
System.out.println(geometry.getArea() + " - " +
geometry.toString() + " - " + totalLength);
}
} finally {
if (iterator != null) {
// YOU MUST CLOSE THE ITERATOR!
iterator.close();
}
}
System.out.println("Total Length " + totalLength);
} catch (IOException ex) {
Logger.getLogger(frmPrincipal.class.getName()).log(Level.SEVERE,
null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(frmPrincipal.class.getName()).log(Level.SEVERE,
null, ex);
}
}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
// Double dLatitude = new Double(-54.470397855929485);
// Double dLongitude = new Double(-25.211777031791751);
Double dLatitude = new Double(-54);
Double dLongitude = new Double(-25);
try {
File f = new File("C:\\ShapesTeste\\BR_MUNICIPIO_IBGE_2006.SHP");
Map<String, Serializable> connectParameters = new HashMap<String,
Serializable>();
connectParameters.put("url", f.toURI().toURL());
connectParameters.put("Cria Index Spacial", true);
try {
DataStore dataStore =
DataStoreFinder.getDataStore(connectParameters);
System.out.println("We connected shape file now");
String[] typeNames = dataStore.getTypeNames();
System.out.println("Reading content " + typeNames[0]);
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
FeatureIterator<SimpleFeature> iterator;
featureSource = dataStore.getFeatureSource(typeNames[0]);
collection = featureSource.getFeatures();
iterator = collection.features();
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
Coordinate c1 = new Coordinate(dLatitude, dLongitude);
Point p = (Point) gf.createPoint(c1);
try {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
ArrayList al = new ArrayList();
al = (ArrayList) feature.getAttributes();
MultiPolygon mp = (MultiPolygon) al.get(0);
System.out.println(p.contains(p));
Coordinate[] coord = mp.getCoordinates();
for (int cCoordenadas = 0; cCoordenadas < coord.length;
cCoordenadas++) {
if (coord[cCoordenadas].x == dLatitude ||
coord[cCoordenadas].y == dLongitude) {
System.out.println("Achei a área!");
System.out.println("Toma ae os resultados.!");
for (int c = 1; c < al.size(); c++) {
System.out.print(al.get(c) + ", ");
}
break;
}
}
// //Sem efeito
// Geometry g = mp.getBoundary();
// g.getNumGeometries();
//
// Point p = g.getInteriorPoint();
// Coordinate c = p.getCoordinate();
}
} finally {
if (iterator != null) {
// YOU MUST CLOSE THE ITERATOR!
iterator.close();
}
}
} catch (IOException ex) {
Logger.getLogger(frmPrincipal.class.getName()).log(Level.SEVERE,
null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(frmPrincipal.class.getName()).log(Level.SEVERE,
null, ex);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
About a = new About();
a.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frmPrincipal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
// End of variables declaration
}
I really need this help.
So, thank you a lot for the attention and assistance.
Paulo Roberto.
--
View this message in context:
http://n2.nabble.com/Geomatry-example-tp1322714p1449131.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users