Objective
One months has not written a blog, today try to write a point.
For a lot of reasons, the most important reason is that I changed my career. Yes, I left the development post and headed for the development of the Predators-product manager. Although the name is the product manager, but the work is also very miscellaneous, in addition to not write code, the others are dry, often have to add a small class, so there is not so much time to study the technical things, mechanical keyboard has dropped a thin layer of dust. But I do love code farming this line, work to see colleagues carefree knock code, the heart is a little itch, so work is still blind, this is not summed up today this article.
About the relationship between product manager and research and development I really have to say a lot, although the two are natural predators, but a research and development of product managers are easy to deal with the development, there are a lot of common topics, each time the project came to my mind about how to solve this matter, and even I have to help the project docking company to solve the development problem
Not much to say, start today's theme, today is mainly about how to convert Shp files to GeoJson, which is easy to implement in QGIS, ArcGIS and other professional software, just a button on the line, this article is to study the story behind this button. This article is encountered in the use of Geotrellis, so still belong to this blog set, of course, the framework is also based on Geotrellis.
One, the realization Mode 1.1 theoretical analysis
In fact, this process is relatively simple, the first to read the Shp file into memory, and then read out the spatial attributes and common properties, the combination of the two in accordance with the format of the GeoJson file can be written.
1.2 Concrete implementations
- Read SHP file
Only one line of code is required to resolve:
val datas = ShapeFileReader.readSimpleFeatures(path)
This is the Geotrellis package good read shp file method, but there is a problem in this way, can not set the reading SHP file encoding, if SHP file is not UTF-8 encoding will have garbled problem, simple transformation of the source code can be achieved:
val datas = { val file = new File(path) val shpDataStore = new ShapefileDataStore(path.getFileUrl()) shpDataStore.setCharset(Charset.forName(charsetName)) val ftItr = shpDataStore.getFeatureSource.getFeatures.features try { val simpleFeatures = mutable.ListBuffer[SimpleFeature]() while (ftItr.hasNext) simpleFeatures += ftItr.next() simpleFeatures } finally { ftItr.close shpDataStore.dispose() }
This makes it possible to read all the contents of the Shp file, with the spatial attribute attached to the normal attribute and, ultimately, the collection of Simplefeature objects.
- Convert content to Feature
The so-called Feature is actually a combination of spatial attributes and common attributes.
def parseAttribute(sf: SimpleFeature) = { import scala.collection.JavaConversions._ sf.getProperties.drop(1).map { p => val attr = sf.getAttribute(p.getName) p.getName.toString -> (if (attr == null) "" else attr.toString) }.toMap } def getGeometryFromSimpleFeature(sf: SimpleFeature) = { val original = sf.getDefaultGeometryProperty.getValue Geometry(original.asInstanceOf[com.vividsolutions.jts.geom.Geometry]) } def toFeature(sf: SimpleFeature) = { Feature(getGeometryFromSimpleFeature(sf), parseAttribute(sf)) }
The above method can convert a single Simplefeature object into a Feature object, and the whole collection only needs to implement the Map method.
- Switch to GeoJson
In the previous step, there must be a classmate very curious, why should be read from the SHP Simplefeature object to Feature object, because of the Feature object, we can easily convert it to GeoJson. Geotrellis has a built-in implicit method of converting the Feature collection to GeoJson, as follows:
implicit class FeaturesToGeoJson[G <: Geometry, D: JsonWriter](features: Traversable[Feature[G, D]]) { def toGeoJson(): String = { JsonFeatureCollection(features).toJson.compactPrint } }
So you can directly convert the Feature set obtained in the second step to GeoJson, as follows:
import geotrellis.vector.io.json.Implicits._val geojson = features.toGeoJson()
Finally, you only need to write the Geojson object to the file.
Second, the change of posture
The above code seems to be an understatement, and it really took me a long time to complete it. Here I have to insert a sentence, there are many students in various ways to consult my questions about the use of Geotrellis, I would like to make a statement here:
First of all, I know the basic is not retained in the blog, about the technical point to ask me and look at my blog almost, I hope with you to explore the realization of ideas and other aspects of the problem;
Secondly, even if you ask me, you have to describe the problem clearly, some students cut a few lines of code to ask why I ran, sorry, first I do not know which article you cut out, and secondly I do not know what you want to do, so I really can not answer;
Third, the code in the article is for the time Geotrellis version, you can see the time may have been updated, there may be no running out of the situation, but generally does not exist, and when there is a simple modification should be possible, and do not need to turn the horn of the point of asking me like 1TB Tiff how to deal with the problem , my article only provides the technical method, does not do the scientific research.
Of course, I am very grateful to all of you for my recognition and support, which is also my continued to write this blog set of one of the driving force (may be less, more and more, the product ...) is the product of the disaster).
Anyway, when I used Scala to toss a few days out, a pat on the head, can't help but scold myself a few words, why I do not directly take Python to achieve this function? Hurriedly researched, found a few lines of code to be done:
from geopandas import *shpdata = GeoDataFrame.from_file(path)features = [shpdata.__geo_interface__]from json import dumpsgeojson = open("demo.json", "w")geojson.write(dumps({"type": "FeatureCollection", \ "features": features}, indent=2) + "\n")geojson.close()
Specific ideas with the above analysis, here is not specifically launched, interested in self-study can be.
As mentioned in the previous article, the strong is not Python but the library, yes, a language with more people, the natural library is rich, and we can choose for their favorite language contribution library, at the same time we cannot cling to a language, should be specific problems specific analysis, Find the most suitable language to solve the problem, of course, every thing is the process of learning, will not be wasted.
Iii. Summary
This paper introduces the way of realizing Shp to GeoJson in two languages, mainly analyzing and solving the problem.
Geotrellis series article link address http://www.cnblogs.com/shoufengwei/p/5619419.html