本文整理汇总了Java中org.gdal.ogr.Layer类的典型用法代码示例。如果您正苦于以下问题:Java Layer类的具体用法?Java Layer怎么用?Java Layer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Layer类属于org.gdal.ogr包,在下文中一共展示了Layer类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createDataSource
import org.gdal.ogr.Layer; //导入依赖的package包/类
public static DataSource createDataSource(final List<Node> nodes,
final List<Edge> edges,
final SpatialReference sourceSpatialReference)
{
final DataSource dataSource = ogr.GetDriverByName("Memory")
.CreateDataSource("vector data");
try
{
final Layer edgeLayer = createEdgeLayer(dataSource,
nodes,
edges,
sourceSpatialReference);
}
catch(final Throwable th)
{
dataSource.delete();
throw th;
}
return dataSource;
}
示例2: reprojectShapeFile
import org.gdal.ogr.Layer; //导入依赖的package包/类
public static void reprojectShapeFile(String pathFile, String outPathFile) {
ogr.RegisterAll();
DataSource vector = ogr.Open(pathFile);
SpatialReference src = new SpatialReference(WKTString.WKT3995);
SpatialReference dst = new SpatialReference(WKTString.WKTWGS84);
CoordinateTransformation tr = new CoordinateTransformation(src, dst);
Driver drv = ogr.GetDriverByName("ESRI Shapefile");
File file = new File(outPathFile);
if (file.exists())
drv.DeleteDataSource(outPathFile);
DataSource outputDs = drv.CreateDataSource(outPathFile);
Layer outLayer = outputDs.CreateLayer(outPathFile, dst);
FieldDefn field_def = new FieldDefn("DN",ogr.OFTInteger);
outLayer.CreateField(field_def);
Layer ll = vector.GetLayer(0);
// Get the Layer's Feature Definition
FeatureDefn featureDefn = ll.GetLayerDefn();
int n = ll.GetFeatureCount();
// outLayer.StartTransaction();
for (int i = 0; i < n; i++) {
Feature feat = ll.GetNextFeature();
Geometry gg = feat.GetGeometryRef();
Geometry newgg = new Geometry(ogr.wkbPolygon);
int srcType = gg.GetGeometryType() ;//& (ogrConstants.wkb25DBit);
if (srcType == ogr.wkbPolygon) {
//int resg = gg.Transform(tr);
newgg.AddGeometry(gg);
} else if (srcType == ogr.wkbMultiPolygon) {
int geomCount = gg.GetGeometryCount();
for (int geomIndex = 0; geomIndex < geomCount; geomIndex++) {
Geometry g=gg.GetGeometryRef(geomIndex);
int resg = g.Transform(tr);
newgg.AddGeometry(g);
}
}
// create a new feature
Feature f = new Feature(featureDefn);
f.SetGeometry(newgg);
// f.SetFID(i);
for (int j = 0; j < feat.GetFieldCount(); j++) {
String type = feat.GetFieldDefnRef(j).GetFieldTypeName(feat.GetFieldType(j));
if (type.equals("String")) {
f.SetField(feat.GetFieldDefnRef(j).GetName(), feat.GetFieldAsString(j));
} else if (type.equals("StringList")) {
} else if (type.equals("Integer")) {
f.SetField(feat.GetFieldDefnRef(j).GetName(), feat.GetFieldAsInteger(j));
} else if (type.equals("IntegerList")) {
} else if (type.equals("Real")) {
f.SetField(feat.GetFieldDefnRef(j).GetName(), feat.GetFieldAsDouble(j));
} else if (type.equals("RealList")) {
}
}
// Add new feature to output Layer
int res = outLayer.CreateFeature(f);
System.out.println("create feature:" + res);
gg.delete();
feat.delete();
}
// outLayer.CommitTransaction();
}
示例3: createEdgeLayer
import org.gdal.ogr.Layer; //导入依赖的package包/类
private static Layer createEdgeLayer(final DataSource dataSource,
final Collection<Node> nodes,
final Iterable<Edge> edges,
final SpatialReference spatialReference)
{
final Map<Integer, Node> nodeMap = new HashMap<>(nodes.size());
for(final Node node : nodes)
{
nodeMap.put(node.getIdentifier(), node);
}
final Layer edgeLayer = dataSource.CreateLayer("edges",
spatialReference,
ogrConstants.wkbLineString);
try
{
for(final Edge edge : edges)
{
final Feature edgeFeature = new Feature(edgeLayer.GetLayerDefn());
final Geometry line = new Geometry(ogrConstants.wkbLineString);
final Node node0 = nodeMap.get(edge.getFrom());
final Node node1 = nodeMap.get(edge.getTo());
line.AddPoint(node0.getX(), node0.getY());
line.AddPoint(node1.getX(), node1.getY());
if(edgeFeature.SetGeometry(line) != gdalconstConstants.CE_None)
{
throw new RuntimeException(new GdalError().getMessage());
}
if(edgeLayer.CreateFeature(edgeFeature) != gdalconstConstants.CE_None)
{
throw new RuntimeException(new GdalError().getMessage());
}
}
}
catch(final Throwable th)
{
edgeLayer.delete();
throw th;
}
return edgeLayer;
}
示例4: renderContours
import org.gdal.ogr.Layer; //导入依赖的package包/类
private static void renderContours(final String contoursFilePath,
final int imageWidth,
final int imageHeight,
final BoundingBox bounds,
final SpatialReference spatialReference,
final Layer layer,
final Color background,
final Color foreground)
{
@SuppressWarnings("UseOfObsoleteCollectionType")
final Vector<String> imageCreationOptions = new Vector<>(1);
imageCreationOptions.add("COMPRESS=LZW");
final Dataset rasterDataset = gdal.GetDriverByName("GTiff")
.Create(contoursFilePath,
imageWidth,
imageHeight,
4, // RGBA
gdalconstConstants.GDT_Byte,
imageCreationOptions);
try
{
rasterDataset.GetRasterBand(1).Fill(background.getRed());
rasterDataset.GetRasterBand(2).Fill(background.getGreen());
rasterDataset.GetRasterBand(3).Fill(background.getBlue());
rasterDataset.GetRasterBand(4).Fill(background.getAlpha());
rasterDataset.SetGeoTransform(new double[]{ bounds.getTopLeft().getX(), // top left x
bounds.getWidth() / (double)imageWidth, // w-e pixel resolution
0, // rotation, 0 if image is "north up"
bounds.getTopLeft().getY(), // top left y
0, // rotation, 0 if image is "north up"
-bounds.getHeight() / (double)imageHeight // n-s pixel resolution (negative value!)
});
rasterDataset.SetProjection(spatialReference.ExportToWkt());
final int rasterizeError = gdal.RasterizeLayer(rasterDataset,
new int[]{1, 2, 3, 4},
layer,
new double[]{ foreground.getRed(),
foreground.getGreen(),
foreground.getBlue(),
foreground.getAlpha()
},
null, // "options" vector. valid choices are described here: http://gdal.org/gdal__alg_8h.html#adfe5e5d287d6c184aab03acbfa567cb1
null);
if(rasterizeError != gdalconstConstants.CE_None)
{
throw new RuntimeException(new GdalError().getMessage());
}
}
finally
{
rasterDataset.delete();
}
}
示例5: checkValid
import org.gdal.ogr.Layer; //导入依赖的package包/类
/**
* Process a file
* @param pFile file to process
* @return true if opened ok, false if not
*/
@SuppressWarnings("unused")
public static boolean checkValid(File pFile) {
if(!pFile.exists()) return false;
final String pszWHERE = null;
final Geometry poSpatialFilter = null;
Driver poDriver = null;
final String pszDataSource = pFile.getAbsolutePath();
/* -------------------------------------------------------------------- */
/* Open data source. */
/* -------------------------------------------------------------------- */
DataSource poDS = ogr.Open(pszDataSource, !bReadOnly);
if (poDS == null && !bReadOnly) {
poDS = ogr.Open(pszDataSource, false);
if (poDS == null && bVerbose) {
if(bVerbose) System.out.println( "Had to open data source read-only.");
}
}
/* -------------------------------------------------------------------- */
/* Report failure */
/* -------------------------------------------------------------------- */
if( poDS == null ) {
if(bVerbose) {
System.out.print("FAILURE: Unable to open datasource `"+pszDataSource+"' with the following drivers.\n");
for( int iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ ) {
System.out.println( " -> " + ogr.GetDriver(iDriver).GetName() );
}
}
return false;
}
poDriver = poDS.GetDriver();
if(!poDriver.GetName().equalsIgnoreCase(OGR_NTF_DRIVER_NAME)) {
// i.e. this is not an NTF file
return false;
}
/* -------------------------------------------------------------------- */
/* Some information messages. */
/* -------------------------------------------------------------------- */
if( bVerbose ) {
System.out.println( "INFO: Open of `" + pszDataSource + "'\n" +
" using driver `" + poDriver.GetName() + "' successful." );
}
/* -------------------------------------------------------------------- */
/* Process each data source layer. */
/* -------------------------------------------------------------------- */
for(int iLayer = 0; iLayer < poDS.GetLayerCount(); iLayer++) {
Layer poLayer = poDS.GetLayer(iLayer);
if(poLayer==null) {
if(bVerbose) System.out.println( "FAILURE: Couldn't fetch advertised layer " + iLayer + "!");
return false;
}
boolean layerOk = reportOnLayer( poLayer, pszWHERE, poSpatialFilter );
if(!layerOk) return false;
}
// if we get this far then the file must be valid(ish)
return true;
}