Java Code Examples for org.geotools.data.DataStoreFinder

The following examples show how to use org.geotools.data.DataStoreFinder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: amodeus   Source File: MultiPolygons.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Set<MultiPolygon> initializeFrom(File shapeFile) throws IOException {
    URL shapeFileURL = shapeFile.toURI().toURL();
    Map<String, URL> inputMap = new HashMap<>();
    inputMap.put("url", shapeFileURL);

    DataStore dataStore = DataStoreFinder.getDataStore(inputMap);
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
    SimpleFeatureCollection collection = DataUtilities.collection(featureSource.getFeatures());
    dataStore.dispose();

    Set<MultiPolygon> polygons = new HashSet<>();
    SimpleFeatureIterator iterator = collection.features();
    while (iterator.hasNext())
        polygons.add((MultiPolygon) iterator.next().getDefaultGeometry());
    return polygons;
}
 
Example 2
Source Project: sldeditor   Source File: DataSourceImpl.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Populates the list of available data stores that can be connected to.
 */
private void populateAvailableDataStores()
{
    DataAccessFactory fac;

    logger.debug("Available data store factories:");

    Iterator<DataStoreFactorySpi> iterator = DataStoreFinder.getAvailableDataStores();
    while (iterator.hasNext()) {
        fac = (DataAccessFactory) iterator.next();

        logger.debug("\t" + fac.getDisplayName());

        availableDataStoreList.add(fac.getDisplayName());
    }
}
 
Example 3
/** Populate name map. */
private static void populateNameMap() {
    Iterator<DataStoreFactorySpi> datastore = DataStoreFinder.getAvailableDataStores();

    while (datastore.hasNext()) {
        DataStoreFactorySpi dSPI = datastore.next();

        Param dbType = null;
        for (Param param : dSPI.getParametersInfo()) {
            if (param.key.equals(JDBCDataStoreFactory.DBTYPE.key)) {
                dbType = param;
                break;
            }
        }
        if (dbType != null) {
            nameMap.put(dSPI.getDisplayName(), (String) dbType.sample);
        }
    }
}
 
Example 4
Source Project: GeoTriples   Source File: ShapeFileParser.java    License: Apache License 2.0 6 votes vote down vote up
public List<GeneralResultRow> getData(String tablename) throws Exception {
	if (cacheList.containsKey(tablename)) {
		return cacheList.get(tablename);
	} else if (cacheList.containsKey(tablename.replaceAll("_geometry", ""))) {
		return cacheList.get(tablename.replaceAll("_geometry", ""));
	}
	DataStore dataStore = null;
	List<GeneralResultRow> result = null;
	try {
		Map<String, URL> connect = new HashMap<String, URL>();
		connect.put("url", shapefile.toURI().toURL());
		dataStore = DataStoreFinder.getDataStore(connect);
		FeatureSource<?, ?> featureSource = dataStore.getFeatureSource(tablename
				.replaceAll("_geometry", ""));
		if (featureSource != null) {
			result = getData(featureSource);
		}
	} catch (Exception ex) {
		ex.printStackTrace();
	} finally {
		dataStore.dispose();
	}
	cacheList.put(tablename.replaceAll("_geometry", ""), result);
	return result;
}
 
Example 5
Source Project: geowave   Source File: TestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static SimpleFeature resourceToFeature(final URL filterResource) throws IOException {
  final Map<String, Object> map = new HashMap<>();
  DataStore dataStore = null;
  map.put("url", filterResource);
  final SimpleFeature savedFilter;
  SimpleFeatureIterator sfi = null;
  try {
    dataStore = DataStoreFinder.getDataStore(map);
    if (dataStore == null) {
      LOGGER.error("Could not get dataStore instance, getDataStore returned null");
      throw new IOException("Could not get dataStore instance, getDataStore returned null");
    }
    // just grab the first feature and use it as a filter
    sfi = dataStore.getFeatureSource(dataStore.getNames().get(0)).getFeatures().features();
    savedFilter = sfi.next();

  } finally {
    if (sfi != null) {
      sfi.close();
    }
    if (dataStore != null) {
      dataStore.dispose();
    }
  }
  return savedFilter;
}
 
Example 6
Source Project: TripleGeo   Source File: ShpConnector.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Loads the shape file from the configuration path and returns the
 * feature collection associated according to the configuration.
 *
 * @param shapePath with the path to the shapefile.
 * @param featureString with the featureString to filter.
 *
 * @return FeatureCollection with the collection of features filtered.
 */
private FeatureCollection getShapeFileFeatureCollection(String shapePath, String featureString) throws IOException 
{
  File file = new File(shapePath);

  // Create the map with the file URL to be passed to DataStore.
  Map map = new HashMap();
  try {
    map.put("url", file.toURL());
  } catch (MalformedURLException ex) {
    Logger.getLogger(ShpConnector.class.getName()).log(Level.SEVERE, null, ex);
  }
  if (map.size() > 0) {
    DataStore dataStore = DataStoreFinder.getDataStore(map);
    FeatureSource featureSource = dataStore.getFeatureSource(featureString);
    return featureSource.getFeatures();
  }
  return null;
}
 
Example 7
Source Project: sldeditor   Source File: DatabaseClient.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean connect() {
    connected = false;

    Map<String, Object> params = getDBConnectionParams();

    try {
        DataStore dataStore = DataStoreFinder.getDataStore(params);

        if (dataStore != null) {
            connectToDatastore(dataStore);
        } else {
            String message =
                    String.format(
                            "%s : %s",
                            Localisation.getString(
                                    DatabaseClient.class, "DatabaseClient.noDriver"),
                            (connection == null)
                                    ? "No connection"
                                    : connection.getConnectionName());
            ConsoleManager.getInstance().error(this, message);
        }
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }

    return connected;
}
 
Example 8
Source Project: sldeditor   Source File: DataSourceImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
/** Populates the list of available data stores that can be connected to. */
private void populateAvailableDataStores() {
    DataAccessFactory fac = null;

    logger.debug("Available data store factories:");

    Iterator<DataStoreFactorySpi> iterator = DataStoreFinder.getAvailableDataStores();
    while (iterator.hasNext()) {
        fac = iterator.next();

        logger.debug("\t" + fac.getDisplayName());

        availableDataStoreList.add(fac.getDisplayName());
    }
}
 
Example 9
Source Project: sldeditor   Source File: DataSourceInfoTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
/** Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getFeatures()}. */
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testGetFeatures() {
    URL url =
            SLDEditorFile.class
                    .getClassLoader()
                    .getResource("point/sld/shp/sld_cookbook_point.shp");

    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);

        DataSourceInfo dsInfo = new DataSourceInfo();

        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();

        assertNull(dsInfo.getFeatures());
        dsInfo.setSchema(schema);

        assertNull(dsInfo.getFeatures());
        dsInfo.setDataStore(dataStore);

        assertTrue(dsInfo.getFeatures() != null);
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 10
Source Project: sldeditor   Source File: DataSourceInfoTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getFeatureCollection()}.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testGetFeatureCollection() {
    URL url =
            SLDEditorFile.class
                    .getClassLoader()
                    .getResource("point/sld/shp/sld_cookbook_point.shp");

    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);

        DataSourceInfo dsInfo = new DataSourceInfo();

        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();

        assertNull(dsInfo.getGeometryFieldName());
        dsInfo.setSchema(schema);

        assertEquals("the_geom", dsInfo.getGeometryFieldName());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 11
Source Project: sldeditor   Source File: DataSourceInfoTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getFeatureStore()}. Test
 * method for {@link
 * com.sldeditor.datasource.impl.DataSourceInfo#setSchema(org.opengis.feature.type.FeatureType)}.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testGetFeatureStore() {
    URL url =
            SLDEditorFile.class
                    .getClassLoader()
                    .getResource("point/sld/shp/sld_cookbook_point.shp");

    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);

        DataSourceInfo dsInfo = new DataSourceInfo();

        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();
        dsInfo.setSchema(schema);

        assertNull(dsInfo.getFeatureStore());
        dsInfo.setDataStore(dataStore);

        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = dsInfo.getFeatureStore();

        assertTrue(featureStore != null);
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 12
Source Project: sldeditor   Source File: DataSourceInfoTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link
 * com.sldeditor.datasource.impl.DataSourceInfo#getPropertyDescriptorList()}.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testGetPropertyDescriptorList() {
    URL url =
            SLDEditorFile.class
                    .getClassLoader()
                    .getResource("point/sld/shp/sld_cookbook_point.shp");

    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);

        DataSourceInfo dsInfo = new DataSourceInfo();

        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();

        assertNull(dsInfo.getPropertyDescriptorList());
        dsInfo.setSchema(schema);

        Collection<PropertyDescriptor> fieldList = dsInfo.getPropertyDescriptorList();

        assertTrue(fieldList.size() == 3);
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 13
Source Project: sldeditor   Source File: DataSourceInfoTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getGeometryFieldName()}.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testGetGeometryFieldName() {
    URL url =
            SLDEditorFile.class
                    .getClassLoader()
                    .getResource("point/sld/shp/sld_cookbook_point.shp");

    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);

        DataSourceInfo dsInfo = new DataSourceInfo();

        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();

        assertNull(dsInfo.getGeometryFieldName());
        dsInfo.setSchema(schema);

        assertEquals("the_geom", dsInfo.getGeometryFieldName());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 14
Source Project: gama   Source File: GamaSqlConnection.java    License: GNU General Public License v3.0 5 votes vote down vote up
public DataStore Connect(final IScope scope) throws Exception {
	final Map<String, Object> connectionParameters = createConnectionParams(scope);
	DataStore dStore;
	dStore = DataStoreFinder.getDataStore(connectionParameters); // get
																	// connection
	// DEBUG.LOG("data store postgress:" + dStore);
	if (dStore == null) { throw new IOException("Can't connect to " + database); }
	return dStore;
}
 
Example 15
Source Project: rya   Source File: GeoMesaGeoIndexer.java    License: Apache License 2.0 5 votes vote down vote up
private static DataStore createDataStore(final Configuration conf) throws IOException {
    // get the configuration parameters
    final Instance instance = ConfigUtils.getInstance(conf);
    final boolean useMock = instance instanceof MockInstance;
    final String instanceId = instance.getInstanceName();
    final String zookeepers = instance.getZooKeepers();
    final String user = ConfigUtils.getUsername(conf);
    final String password = ConfigUtils.getPassword(conf);
    final String auths = ConfigUtils.getAuthorizations(conf).toString();
    final String tableName = getTableName(conf);
    final int numParitions = OptionalConfigUtils.getGeoNumPartitions(conf);

    final String featureSchemaFormat = "%~#s%" + numParitions + "#r%" + FEATURE_NAME
            + "#cstr%0,3#gh%yyyyMMdd#d::%~#s%3,2#gh::%~#s%#id";
    // build the map of parameters
    final Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put("instanceId", instanceId);
    params.put("zookeepers", zookeepers);
    params.put("user", user);
    params.put("password", password);
    params.put("auths", auths);
    params.put("tableName", tableName);
    params.put("indexSchemaFormat", featureSchemaFormat);
    params.put("useMock", Boolean.toString(useMock));

    // fetch the data store from the finder
    return DataStoreFinder.getDataStore(params);
}
 
Example 16
private static FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource(File file) throws IOException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
    map.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.TRUE);
    DataStore shapefileStore = DataStoreFinder.getDataStore(map);
    String typeName = shapefileStore.getTypeNames()[0]; // Shape files do only have one type name
    FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
    featureSource = shapefileStore.getFeatureSource(typeName);
    return featureSource;
}
 
Example 17
Source Project: searoute   Source File: SeaRouting.java    License: European Union Public License 1.2 4 votes vote down vote up
public SeaRouting(int resKM) {
	try {

		//load marnet
		DataStore store = null;
		SimpleFeatureCollection fc = null;
		/*try {
			URL url = getClass().getResource("/marnet/marnet_plus_"+resKM+"km.gpkg");
			HashMap<String, Object> map = new HashMap<>();
			map.put(GeoPkgDataStoreFactory.DBTYPE.key, "geopkg");
			map.put(GeoPkgDataStoreFactory.DATABASE.key, url.getFile());
			map.put("url", url);
			store = DataStoreFinder.getDataStore(map);
			fc = store.getFeatureSource(store.getTypeNames()[0]).getFeatures();
		} catch (Exception e) {*/
			//URL url = new URL(path);
			HashMap<String, Object> map = new HashMap<>();
			map.put(GeoPkgDataStoreFactory.DBTYPE.key, "geopkg");
			map.put(GeoPkgDataStoreFactory.DATABASE.key, "marnet/marnet_plus_"+resKM+"km.gpkg");
			map.put("url", "marnet/marnet_plus_"+resKM+"km.gpkg");
			store = DataStoreFinder.getDataStore(map);
			fc = store.getFeatureSource(store.getTypeNames()[0]).getFeatures();
		//}

		//build graph
		FeatureIterator<?> it = fc.features();
		FeatureGraphGenerator gGen = new FeatureGraphGenerator(new LineStringGraphGenerator());
		while(it.hasNext()) gGen.add(it.next());
		g = gGen.getGraph();
		it.close();
		store.dispose();
	} catch (Exception e) { e.printStackTrace(); }

	//link nodes around the globe
	for(Object o : g.getNodes()){
		Node n = (Node)o;
		Coordinate c = ((Point)n.getObject()).getCoordinate();
		if(c.x==180) {
			Node n_ = getNode(new Coordinate(-c.x,c.y));
			if(LOGGER.isTraceEnabled()) LOGGER.trace(c + " -> " + ((Point)n_.getObject()).getCoordinate());
			BasicEdge be = new BasicEdge(n, n_);
			n.add(be);
			n_.add(be);
			g.getEdges().add(be);
		}
	}

	//define weighters
	defaultWeighter = buildEdgeWeighter(true, true);
	noSuezWeighter = buildEdgeWeighter(false, true);
	noPanamaWeighter = buildEdgeWeighter(true, false);
	noSuezNoPanamaWeighter = buildEdgeWeighter(false, false);
}
 
Example 18
protected DataStore getDataStoreForDatasource(Datasource datasource) throws IOException {
    return DataStoreFinder.getDataStore(getParamsForDatasource(datasource));
}
 
Example 19
/**
 * Connect.
 *
 * @param typeName the type name
 * @param geometryFieldName the geometry field name
 * @param editorFile the editor file
 * @return the list of datastores
 */
@Override
public List<DataSourceInfo> connect(
        String typeName, String geometryFieldName, SLDEditorFileInterface editorFile) {
    List<DataSourceInfo> dataSourceInfoList = new ArrayList<>();
    dataSourceInfoList.add(dsInfo);

    dsInfo.reset();

    if (editorFile != null) {
        SLDDataInterface sldData = editorFile.getSLDData();

        DataSourcePropertiesInterface dataSourceProperties = sldData.getDataSourceProperties();

        Map<String, Object> map = dataSourceProperties.getConnectionProperties();

        if (dataSourceProperties.hasPassword()) {
            String password = dataSourceProperties.getPassword();

            if (password == null) {
                dataSourceProperties.setPassword("dummy password");

                map = dataSourceProperties.getConnectionProperties();
            }
        }

        DataStore dataStore = null;
        try {
            dataStore = DataStoreFinder.getDataStore(map);

            if (dataStore != null) {
                // Try connecting to a vector data source
                connectToVectorDataSource(typeName, dataStore);
            } else {
                // Try connecting to a raster data source
                connectToRasterDataSource(map);
            }
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }

        dsInfo.setDataStore(dataStore);

        if (!dsInfo.hasData()) {
            ConsoleManager.getInstance()
                    .error(
                            this,
                            Localisation.getField(
                                            CreateExternalDataSource.class,
                                            "CreateExternalDataSource.failedToConnect")
                                    + dataSourceProperties.getDebugConnectionString());
        }
    }
    return dataSourceInfoList;
}
 
Example 20
Source Project: GeoTriples   Source File: ShapefileMappingGenerator.java    License: Apache License 2.0 4 votes vote down vote up
public void run() throws IOException {
	Map<String, URL> connect = new HashMap<String, URL>();
	connect.put("url", new File(pathToShapefile).toURI().toURL());
	DataStore dataStore = DataStoreFinder.getDataStore(connect);
	String[] typeNames = dataStore.getTypeNames();
	for (int i = 0; i < typeNames.length; ++i) {
		String typeName = typeNames[i];
		triplesMaps.put(typeName, "");
		triplesMaps.put(typeName, triplesMaps.get(typeName) + printTriplesMap(typeName));
		triplesMaps.put(typeName, triplesMaps.get(typeName) + printLogicalSource(typeName));
		triplesMaps.put(typeName, triplesMaps.get(typeName) + printSubjectMap(baseURI, typeName));
		FeatureSource<?, ?> featureSource = dataStore.getFeatureSource(typeName);

		FeatureType ft = featureSource.getSchema();
		String typeNameGeo = typeNames[i] + "_Geometry";
		for (PropertyDescriptor property : ft.getDescriptors()) {

			String identifier = property.getName().getLocalPart();
			if (identifier.equals("the_geom")) {
				continue;
			}
			String datatype = TranslateDataTypeToXSD((property.getType().getBinding().getName()));
			Query q = new Query();
			triplesMaps.put(typeName, triplesMaps.get(typeName)
					+ printPredicateObjectMap(identifier, identifier, datatype, typeName));

		}
		// triplesMaps.put(typeName,
		// triplesMaps.get(typeName) + printPredicateObjectMap(true,
		// "hasGeometry",
		// baseURI + (baseURI.endsWith("/") ? "" : "/") + typeNameGeo +
		// "/{GeoTriplesID}", null,
		// typeName, true));
		triplesMaps
				.put(typeName,
						triplesMaps.get(typeName) + printPredicateObjectMap(true, "hasGeometry",
								baseURI + (baseURI.endsWith("/") ? "" : "/") + typeName
										+ "/Geometry/{GeoTriplesID}",
								null, null, "ogc", null, typeName, true, false));
		triplesMaps.put(typeNameGeo, "");
		triplesMaps.put(typeNameGeo, triplesMaps.get(typeNameGeo) + printTriplesMap(typeNameGeo));
		triplesMaps.put(typeNameGeo, triplesMaps.get(typeNameGeo) + printLogicalSource(typeName));
		triplesMaps.put(typeNameGeo, triplesMaps.get(typeNameGeo) + printSubjectMap(baseURI, typeName, null, true));
		triplesMaps.put(typeNameGeo, triplesMaps.get(typeNameGeo) + printGEOPredicateObjectMaps());
	}
	printmapping();
	printontology();
}
 
Example 21
Source Project: GeoTriples   Source File: ShapeFileParser.java    License: Apache License 2.0 4 votes vote down vote up
public List<TableDef> getTablesDefs() throws Exception {
	DataStore dataStore = null;
	List<ColumnDef> columns = null;
	TableDef onlytable = null;
	Set<Key> primkeys = new HashSet<Key>();
	List<TableDef> tables = new ArrayList<TableDef>();
	try {
		Map<String, URL> connect = new HashMap<String, URL>();
		connect.put("url", shapefile.toURI().toURL());
		dataStore = DataStoreFinder.getDataStore(connect);
		String[] typeNames = dataStore.getTypeNames();
		for (int i = 0; i < typeNames.length; ++i) {
			String typeName = typeNames[i];
			FeatureSource<?, ?> featureSource = dataStore
					.getFeatureSource(typeName);

			FeatureType ft = featureSource.getSchema();
			columns = new ArrayList<ColumnDef>();
			for (PropertyDescriptor property : ft.getDescriptors()) {

				Identifier identifier = Identifier.createDelimited(property
						.getName().getLocalPart());
				DataType datatype = TableDefUtils.TranslateDataTypeToSQLType((property
						.getType().getBinding().getName()));
				ColumnDef col = new ColumnDef(identifier, datatype,
						property.isNillable());
				columns.add(col);
			}
			//Identifier identifier = Identifier.createDelimited("gid");
			//ColumnDef col = new ColumnDef(identifier, TranslateDataTypeToSQLType("Int"), false);
			//columns.add(col);
			//primkeys.add(Key.create(identifier));
			TableName tablename = TableName.create(null, null,
					Identifier.create(true, dataStore.getTypeNames()[0]));
			onlytable = new TableDef(tablename, columns, null, primkeys,
					new HashSet<ForeignKey>());
			tables.add(onlytable);
		}

	} catch (Throwable e) {
		throw new Exception(e.getMessage());
	} finally {
		dataStore.dispose();

	}
	return tables;

}
 
Example 22
Source Project: GeoTriples   Source File: ShapeFileParserGDAL.java    License: Apache License 2.0 4 votes vote down vote up
public List<TableDef> getTablesDefs() throws Exception {
	DataStore dataStore = null;
	List<ColumnDef> columns = null;
	TableDef onlytable = null;
	Set<Key> primkeys = new HashSet<Key>();
	List<TableDef> tables = new ArrayList<TableDef>();
	try {
		Map<String, URL> connect = new HashMap<String, URL>();
		connect.put("url", shapefile.toURI().toURL());
		dataStore = DataStoreFinder.getDataStore(connect);
		String[] typeNames = dataStore.getTypeNames();
		for (int i = 0; i < typeNames.length; ++i) {
			String typeName = typeNames[i];
			FeatureSource<?, ?> featureSource = dataStore.getFeatureSource(typeName);

			FeatureType ft = featureSource.getSchema();
			columns = new ArrayList<ColumnDef>();
			for (PropertyDescriptor property : ft.getDescriptors()) {

				Identifier identifier = Identifier.createDelimited(property.getName().getLocalPart());
				DataType datatype = TableDefUtils
						.TranslateDataTypeToSQLType((property.getType().getBinding().getName()));
				ColumnDef col = new ColumnDef(identifier, datatype, property.isNillable());
				columns.add(col);
			}
			// Identifier identifier = Identifier.createDelimited("gid");
			// ColumnDef col = new ColumnDef(identifier,
			// TranslateDataTypeToSQLType("Int"), false);
			// columns.add(col);
			// primkeys.add(Key.create(identifier));
			TableName tablename = TableName.create(null, null,
					Identifier.create(true, dataStore.getTypeNames()[0]));
			onlytable = new TableDef(tablename, columns, null, primkeys, new HashSet<ForeignKey>());
			tables.add(onlytable);
		}

	} catch (Throwable e) {
		throw new Exception(e.getMessage());
	} finally {
		dataStore.dispose();

	}
	return tables;

}
 
Example 23
Source Project: geowave   Source File: WRS2GeometryStore.java    License: Apache License 2.0 4 votes vote down vote up
private void init() throws MalformedURLException, IOException {
  if (!wrs2Shape.exists()) {
    if (!wrs2Directory.delete()) {
      LOGGER.warn("Unable to delete '" + wrs2Directory.getAbsolutePath() + "'");
    }
    final File wsDir = wrs2Directory.getParentFile();
    if (!wsDir.exists() && !wsDir.mkdirs()) {
      LOGGER.warn("Unable to create directory '" + wsDir.getAbsolutePath() + "'");
    }

    if (!wrs2Directory.mkdirs()) {
      LOGGER.warn("Unable to create directory '" + wrs2Directory.getAbsolutePath() + "'");
    }
    // download and unzip the shapefile
    final File targetFile = new File(wrs2Directory, WRS2_SHAPE_ZIP);
    if (targetFile.exists()) {
      if (!targetFile.delete()) {
        LOGGER.warn("Unable to delete file '" + targetFile.getAbsolutePath() + "'");
      }
    }
    FileUtils.copyURLToFile(new URL(WRS2_SHAPE_URL), targetFile);
    final ZipFile zipFile = new ZipFile(targetFile);
    try {
      final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
      while (entries.hasMoreElements()) {
        final ZipArchiveEntry entry = entries.nextElement();
        if (!entry.isDirectory()) {
          FileUtils.copyInputStreamToFile(
              zipFile.getInputStream(entry),
              new File(wrs2Directory, entry.getName()));
          // HP Fortify "Path Traversal" false positive
          // What Fortify considers "user input" comes only
          // from users with OS-level access anyway
        }
      }
    } finally {
      zipFile.close();
    }
  }
  // read the shapefile and cache the features for quick lookup by path
  // and row
  try {
    final Map<String, Object> map = new HashMap<>();
    map.put("url", wrs2Shape.toURI().toURL());
    final DataStore dataStore = DataStoreFinder.getDataStore(map);
    if (dataStore == null) {
      LOGGER.error("Unable to get a datastore instance, getDataStore returned null");
      return;
    }
    final SimpleFeatureSource source = dataStore.getFeatureSource(WRS2_TYPE_NAME);

    final SimpleFeatureCollection featureCollection = source.getFeatures();
    wrs2Type = featureCollection.getSchema();
    final SimpleFeatureIterator iterator = featureCollection.features();
    while (iterator.hasNext()) {
      final SimpleFeature feature = iterator.next();
      final Number path = (Number) feature.getAttribute("PATH");
      final Number row = (Number) feature.getAttribute("ROW");
      featureCache.put(
          new WRS2Key(path.intValue(), row.intValue()),
          (MultiPolygon) feature.getDefaultGeometry());
    }
  } catch (final IOException e) {
    LOGGER.error(
        "Unable to read wrs2_asc_desc shapefile '" + wrs2Shape.getAbsolutePath() + "'",
        e);
    throw (e);
  }
}
 
Example 24
Source Project: geowave   Source File: TestUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static ExpectedResults getExpectedResults(
    final URL[] expectedResultsResources,
    final CoordinateReferenceSystem crs) throws IOException {
  final Map<String, Object> map = new HashMap<>();
  DataStore dataStore = null;
  final Set<Long> hashedCentroids = new HashSet<>();
  int expectedResultCount = 0;
  final MathTransform mathTransform = transformFromCrs(crs);
  final TWKBWriter writer = new TWKBWriter();
  final TWKBReader reader = new TWKBReader();
  for (final URL expectedResultsResource : expectedResultsResources) {
    map.put("url", expectedResultsResource);
    SimpleFeatureIterator featureIterator = null;
    try {
      dataStore = DataStoreFinder.getDataStore(map);
      if (dataStore == null) {
        LOGGER.error("Could not get dataStore instance, getDataStore returned null");
        throw new IOException("Could not get dataStore instance, getDataStore returned null");
      }
      final SimpleFeatureCollection expectedResults =
          dataStore.getFeatureSource(dataStore.getNames().get(0)).getFeatures();

      expectedResultCount += expectedResults.size();
      // unwrap the expected results into a set of features IDs so its
      // easy to check against
      featureIterator = expectedResults.features();
      while (featureIterator.hasNext()) {
        final SimpleFeature feature = featureIterator.next();
        final Geometry geometry = (Geometry) feature.getDefaultGeometry();

        // TODO: Geometry has to be serialized and deserialized here
        // to make the centroid match the one coming out of the
        // database.
        final long centroid =
            hashCentroid(
                reader.read(
                    writer.write(
                        mathTransform != null ? JTS.transform(geometry, mathTransform)
                            : geometry)));
        hashedCentroids.add(centroid);
      }
    } catch (MismatchedDimensionException | TransformException | ParseException e) {
      LOGGER.warn("Unable to transform geometry", e);
      Assert.fail("Unable to transform geometry to CRS: " + crs.toString());
    } finally {
      IOUtils.closeQuietly(featureIterator);
      if (dataStore != null) {
        dataStore.dispose();
      }
    }
  }
  return new ExpectedResults(hashedCentroids, expectedResultCount);
}
 
Example 25
Source Project: geowave   Source File: BasicMapReduceIT.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testIngestAndQueryGeneralGpx() throws Exception {
  TestUtils.deleteAll(dataStorePluginOptions);
  MapReduceTestUtils.testMapReduceIngest(
      dataStorePluginOptions,
      DimensionalityType.SPATIAL,
      GENERAL_GPX_INPUT_GPX_DIR);
  final File gpxInputDir = new File(GENERAL_GPX_INPUT_GPX_DIR);
  final File expectedResultsDir = new File(GENERAL_GPX_EXPECTED_RESULTS_DIR);
  final List<URL> expectedResultsResources = new ArrayList<>();
  final Map<String, URL> baseNameToExpectedResultURL = new HashMap<>();

  for (final File file : expectedResultsDir.listFiles(new FileFilter() {

    @Override
    public boolean accept(final File pathname) {
      final Map<String, Object> map = new HashMap<>();
      try {
        map.put("url", pathname.toURI().toURL());
        return DataStoreFinder.getDataStore(map) != null;
      } catch (final IOException e) {
        LOGGER.warn("Cannot read file as GeoTools data store", e);
      }
      return false;
    }
  })) {
    baseNameToExpectedResultURL.put(
        FilenameUtils.getBaseName(file.getName()).replaceAll("_filtered", ""),
        file.toURI().toURL());
  }
  for (final String filename : gpxInputDir.list(new FilenameFilter() {
    @Override
    public boolean accept(final File dir, final String name) {
      return FilenameUtils.isExtension(name, new GpxIngestPlugin().getFileExtensionFilters());
    }
  })) {
    final URL url = baseNameToExpectedResultURL.get(FilenameUtils.getBaseName(filename));
    Assert.assertNotNull(url);
    expectedResultsResources.add(url);
  }
  final ExpectedResults expectedResults =
      TestUtils.getExpectedResults(
          expectedResultsResources.toArray(new URL[expectedResultsResources.size()]));
  runTestJob(
      expectedResults,
      TestUtils.resourceToQuery(new File(GENERAL_GPX_FILTER_FILE).toURI().toURL()),
      null,
      null);
}
 
Example 26
/**
 * Creates a suitable {@link DataStore} for the specified parameters.
 * 
 * @param parameters list of GeoTools parameters.
 * @return data store, never null
 * @throws IOException could not create data store
 */
public static DataStore create(Map<String, Object> parameters) throws IOException {
	Object url = parameters.get(ShapefileDataStoreFactory.URLP.key);
	Logger log = LoggerFactory.getLogger(DataStoreFactory.class);
	if (url instanceof String) {
		parameters.put(ShapefileDataStoreFactory.URLP.key, ResourceUtils.getURL((String) url).toExternalForm());
	}
	if (DATASTORE_CACHE.containsKey(parameters)) {
		return DATASTORE_CACHE.get(parameters);
	}
	DataStore store = DataStoreFinder.getDataStore(parameters);
	Object typed = parameters.get(USE_TYPED_FIDS);
	if (typed instanceof String) {
		Boolean t = Boolean.valueOf((String) typed);
		if (!t) {
			if (store != null) {
				log.warn("Non-typed FIDs are only supported by first-generation JDBC datastores, "
						+ "using default fid format for datastore class " + store.getClass().getName());
			}
		}
	}
	if (null == store) {
		StringBuilder availableStr = new StringBuilder();
		StringBuilder missingStr = new StringBuilder();
		Iterator<DataStoreFactorySpi> all = DataStoreFinder.getAllDataStores();
		while (all.hasNext()) {
			DataStoreFactorySpi factory = all.next();
			if (!factory.isAvailable()) {
				log.warn("Datastore factory " + factory.getDisplayName() + "(" + factory.getDescription()
						+ ") is not available");
				if (missingStr.length() != 0) {
					missingStr.append(",");
				}
				missingStr.append(factory.getDisplayName());
			} else {
				if (availableStr.length() != 0) {
					availableStr.append(",");
				}
				availableStr.append(factory.getDisplayName());
			}
		}
		throw new IOException(
				"No datastore found. Possible causes are missing factory or missing library for your datastore"
						+ " (e.g. database driver).\nCheck the isAvailable() method of your"
						+ " DataStoreFactory class to find out which libraries are needed.\n"
						+ "Unavailable factories : " + missingStr + "\n" + "Available factories : " + availableStr
						+ "\n");
	}
	DATASTORE_CACHE.put(parameters, store);
	return store;
}