I’m trying up to run the following python code to extract the wkt from shapefile, as I’m new python user, I got an error as following, still don’t know how to use snappy to do so?, Please any suggestions,
import shapefile
import pygeoif
r = shapefile.Reader(“D:\STUDY\Sh.files\ROI\ROI-2019.shp”)
g=[]
for s in r.shapes():
g.append(pygeoif.geometry.as_shape(s))
m = pygeoif.MultiPoint(g)
print m.wkt
I solved it, the last one pirnt m.wkt should be print(m.wkt), But still if some hints to run the same using snappy!
import shapefile
import pygeoif
r = shapefile.Reader(“D:\STUDY\Sh.files\ROI\ROI-2019.shp”)
g=[]
for s in r.shapes():
g.append(pygeoif.geometry.as_shape(s))
m = pygeoif.MultiPoint(g)
print (m.wkt)
marpet
August 14, 2019, 9:39am
3
Hi Falah,
what needs to be done to read a shapefile can be seen in this Java snippet:
DefaultFeatureCollection featureCollection = FeatureUtils.loadShapefileForProduct(file,
product,
crsProvider,
pm);
Style[] styles = SLDUtils.loadSLD(file);
ProductNodeGroup<VectorDataNode> vectorDataGroup = product.getVectorDataGroup();
String name = VectorDataNodeImporter.findUniqueVectorDataNodeName(featureCollection.getSchema().getName().getLocalPart(),
vectorDataGroup);
if (styles.length > 0) {
SimpleFeatureType featureType = SLDUtils.createStyledFeatureType(featureCollection.getSchema());
VectorDataNode vectorDataNode = new VectorDataNode(name, featureType);
DefaultFeatureCollection styledCollection = vectorDataNode.getFeatureCollection();
String defaultCSS = vectorDataNode.getDefaultStyleCss();
SLDUtils.applyStyle(styles[0], defaultCSS, featureCollection, styledCollection);
return vectorDataNode;
} else {
return new VectorDataNode(name, featureCollection);
}
The reads the shapefil and creates a VectorDataNode which can be added to the product.
Simpler is just the reading of the shapefile intto Feature objects.
public static FeatureCollection<SimpleFeatureType, SimpleFeature> loadFeatureCollectionFromShapefile(File shapefile) throws IOException {
final URL shapefileUrl = shapefile.toURI().toURL();
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = getFeatureSource(shapefileUrl);
return featureSource.getFeatures();
}
The conversion to WKT string can be seen here:
private void exportToWkt() {
SimpleFeatureFigure selectedFeatureFigure = getSimpleFeatureFigure();
if (selectedFeatureFigure == null) {
Dialogs.showInformation(DLG_TITLE, "Please select a geometry.", null);
return;
}
SimpleFeature simpleFeature = selectedFeatureFigure.getSimpleFeature();
CoordinateReferenceSystem sourceCrs = simpleFeature.getDefaultGeometryProperty().getDescriptor().getCoordinateReferenceSystem();
CoordinateReferenceSystem targetCrs = DefaultGeographicCRS.WGS84;
Geometry sourceGeom = selectedFeatureFigure.getGeometry();
Geometry targetGeom;
try {
targetGeom = transformGeometry(sourceGeom, sourceCrs, targetCrs);
} catch (Exception e) {
Dialogs.showWarning(DLG_TITLE, "Failed to transform geometry to " + targetCrs.getName() + ".\n" +
"Using " + sourceCrs.getName() + " instead.", null);
targetGeom = sourceGeom;
targetCrs = sourceCrs;
}
This file has been truncated. show original
So I think your code is simple
2 Likes
Thanks a lot for your great assistance, New user is blind in the beginning, but I promise I’ll open my four eyes
marpet
August 14, 2019, 9:59am
5
It’s hard to find as long as you don’t know where to look for it.