Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to extract polygons in shp #112

Open
JeanCollas opened this issue Dec 2, 2020 · 0 comments
Open

How to extract polygons in shp #112

JeanCollas opened this issue Dec 2, 2020 · 0 comments

Comments

@JeanCollas
Copy link

I have tried many times in different ways, I have not been able to extract valid polygons shp from for example regions of a country.

Some of what I have tried so far :

public void Extract(string fileNameIn, string folderOut)
{
	using (var fileStream = new FileInfo(fileNameIn).OpenRead())
	{
		var source = new PBFOsmStreamSource(fileStream);

		var f = (from s in source
				 where s.Tags.Contains("boundary", "administrative")
				 where s.Tags.Contains("admin_level", "3")
				 where s.Tags.ContainsKey("name")
				 where !s.Tags.ContainsKey("highway")
				 select s)
                                // .Take(1)
				 .ToFeatureSource();

		var fileOut = Path.Combine(folderOut, f.ToString());

		// build feature collection.
		var featureCollection = new FeatureCollection();
		var attributesTable = new AttributesTable();

		var geoCount = 0;
		foreach (var feature in f)
		{ // make sure there is a constant # of attributes with the same names before writing the shapefile.
			featureCollection.Add(new Feature(feature.Geometry, attributesTable));
			geoCount++;
		}
geoCount.Dump();
		if (geoCount > 0)
		{


			// convert to shape.
			var header = NetTopologySuite.IO.ShapefileDataWriter.GetHeader(featureCollection.First(), featureCollection.Count);
			var shapeWriter = new ShapefileDataWriter(fileOut + ".shp", new GeometryFactory())
			{
				Header = header
			};
			shapeWriter.Write(featureCollection);
		}
	}
}

This takes ~15min applied to an extract of France, but no relevant result.

Any recommendation ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant