first version

This commit is contained in:
H.T. Kruitbosch 2018-09-27 15:37:18 +02:00
commit 210791f96d
4 changed files with 272 additions and 0 deletions

188
Readme.md Normal file
View File

@ -0,0 +1,188 @@
# gabmap tools
Gabmap is a tool to crate linguistics maps, found here [https://www.gabmap.nl/](https://www.gabmap.nl/).
This package provides tools to prepare gabmap data needed by the above website.
(Currently) Only supports conversion from geojson to gabmap-supported kml. Tested on Python3.6, probably also works in 2.7 and 3.x
## Installation
pip install gabmap
## Example
from gabmap import as_gabmap_kml
example_geojson = {
'features': [
{
'type': 'Feature',
'properties': {'ignored_property': 9000, 'name': 'Just a point'},
'geometry': {
'type': 'Point',
'coordinates': [5.97218371298345, 53.321051722346],
}
}, {
'type': 'Feature',
'properties': {'ignored_property': 9000, 'name': 'Dongeradeel, Dokkum'},
'geometry': {
'type': 'Polygon',
'coordinates': [[
[5.972183712989529, 53.321051729937366],
[5.972649444783667, 53.323459310428774],
[5.974617105244615, 53.323094551891245],
[5.971118458390794, 53.33136874824667],
[5.97290140328097, 53.33401217805459],
[5.988828240487099, 53.33466079566186],
[5.989652143461869, 53.336091863094985],
[5.995516626556427, 53.3347625832367],
[6.013581255175037, 53.33771098627605],
[6.014977791112734, 53.33648059762006],
[6.017554860173514, 53.337095328467285],
[6.019363005880393, 53.3350016111794],
[6.024782600074181, 53.33467072960145],
[6.024756446131901, 53.33273031950157],
[6.026498893390842, 53.332221897746145],
[6.023034030721839, 53.32918544731246],
[6.022876144124088, 53.32602381606865],
[6.024719847213362, 53.32464644597469],
[6.022437903780341, 53.32403096499117],
[6.022954478786472, 53.32210829964382],
[6.02124132025574, 53.320333445464456],
[6.020151965590102, 53.30822359787938],
[5.998101020604852, 53.310500200623295],
[5.975852806584123, 53.30692731635925],
[5.970668783608778, 53.31367328171959],
[5.97429990442482, 53.31599867168855],
[5.972183712989529, 53.321051729937366]
]]
}
}, {
'type': 'Feature',
'properties': {'yet_another_ignored_property': 12, 'name': 'Dongeradeel, Metslawier'},
'geometry': {
'type': 'Polygon',
'coordinates': [[
[6.046967750144655, 53.371937895154524],
[6.0549222966439515, 53.371918076451],
[6.059352072721314, 53.36890643371235],
[6.062417104803104, 53.37132443327753],
[6.067905957583034, 53.371022203976416],
[6.0687084280154, 53.374887587170115],
[6.073009736850938, 53.3788555365375],
[6.078403346142309, 53.374857801982635],
[6.072748066948085, 53.37023019614317],
[6.075265557823207, 53.36579886593933],
[6.086521267398217, 53.363239356148796],
[6.082574229596272, 53.35279822069439],
[6.094621430086393, 53.353315989005495],
[6.080085673717988, 53.34335756890432],
[6.08081474997191, 53.34237156076051],
[6.066679950835733, 53.33685889893402],
[6.065234168475946, 53.33835014728265],
[6.062546118146774, 53.3376267442749],
[6.063205060821552, 53.33530061406535],
[6.066655308724881, 53.33344964672852],
[6.065649785255536, 53.331609521110835],
[6.067338586918196, 53.330997322012905],
[6.065897005115673, 53.33004223482746],
[6.067404400613272, 53.327763855588465],
[6.066227593986628, 53.32751948129203],
[6.071219889834814, 53.32470721385325],
[6.066774308653418, 53.32363525036028],
[6.052876005968958, 53.32492390130383],
[6.035816537863838, 53.324268920519664],
[6.03296256575084, 53.325630418778424],
[6.024599190974769, 53.32459415257276],
[6.022839175519536, 53.32764410733526],
[6.023531219120203, 53.330156874845095],
[6.026498893390842, 53.332221897746145],
[6.024756446131901, 53.33273031950157],
[6.024782600074181, 53.33467072960145],
[6.019363005880393, 53.3350016111794],
[6.017554860173514, 53.337095328467285],
[6.014977791112734, 53.33648059762006],
[6.014092972385168, 53.33765968957119],
[5.995516626556427, 53.3347625832367],
[5.993100803235959, 53.3362054938723],
[5.989652143461869, 53.336091863094985],
[5.988828240487099, 53.33466079566186],
[5.982843753781665, 53.33523634257272],
[5.9827015425321495, 53.338134792094905],
[5.987148944429796, 53.338733894585175],
[5.991707200958003, 53.34276042605685],
[5.98904704189495, 53.34456541977238],
[5.9898349659695755, 53.34834022603107],
[6.0062691176055925, 53.3511085885891],
[6.011797802445011, 53.34664176084195],
[6.018332928979364, 53.347611115387366],
[6.023471458821317, 53.352392212983766],
[6.021372827495325, 53.35663887555595],
[6.020588676561126, 53.365296580714286],
[6.029768573248898, 53.368983093660454],
[6.0392376282345115, 53.369450978110535],
[6.045416701652879, 53.37286274340197],
[6.046967750144655, 53.371937895154524]
]]
},
}
]
}
print(as_gabmap_kml(example_geojson, document_name='Happy KML', name_property='name'))
## Result
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Happy KML</name>
<visibility>1</visibility>
<Placemark>
<name>Just a point</name>
<visibility>1</visibility>
<Point>
<coordinates>5.972184,53.321052</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Dongeradeel, Dokkum</name>
<visibility>1</visibility>
<Point>
<coordinates>5.997798,53.322503</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Dongeradeel, Dokkum</name>
<visibility>1</visibility>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>5.972184,53.321052 5.972649,53.323459 5.974617,53.323095 5.971118,53.331369 5.972901,53.334012 5.988828,53.334661 5.989652,53.336092 5.995517,53.334763 6.013581,53.337711 6.014978,53.336481 6.017555,53.337095 6.019363,53.335002 6.024783,53.334671 6.024756,53.332730 6.026499,53.332222 6.023034,53.329185 6.022876,53.326024 6.024720,53.324646 6.022438,53.324031 6.022954,53.322108 6.021241,53.320333 6.020152,53.308224 5.998101,53.310500 5.975853,53.306927 5.970669,53.313673 5.974300,53.315999 5.972184,53.321052</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
<Placemark>
<name>Dongeradeel, Metslawier</name>
<visibility>1</visibility>
<Point>
<coordinates>6.044516,53.348356</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Dongeradeel, Metslawier</name>
<visibility>1</visibility>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>6.046968,53.371938 6.054922,53.371918 6.059352,53.368906 6.062417,53.371324 6.067906,53.371022 6.068708,53.374888 6.073010,53.378856 6.078403,53.374858 6.072748,53.370230 6.075266,53.365799 6.086521,53.363239 6.082574,53.352798 6.094621,53.353316 6.080086,53.343358 6.080815,53.342372 6.066680,53.336859 6.065234,53.338350 6.062546,53.337627 6.063205,53.335301 6.066655,53.333450 6.065650,53.331610 6.067339,53.330997 6.065897,53.330042 6.067404,53.327764 6.066228,53.327519 6.071220,53.324707 6.066774,53.323635 6.052876,53.324924 6.035817,53.324269 6.032963,53.325630 6.024599,53.324594 6.022839,53.327644 6.023531,53.330157 6.026499,53.332222 6.024756,53.332730 6.024783,53.334671 6.019363,53.335002 6.017555,53.337095 6.014978,53.336481 6.014093,53.337660 5.995517,53.334763 5.993101,53.336205 5.989652,53.336092 5.988828,53.334661 5.982844,53.335236 5.982702,53.338135 5.987149,53.338734 5.991707,53.342760 5.989047,53.344565 5.989835,53.348340 6.006269,53.351109 6.011798,53.346642 6.018333,53.347611 6.023471,53.352392 6.021373,53.356639 6.020589,53.365297 6.029769,53.368983 6.039238,53.369451 6.045417,53.372863 6.046968,53.371938</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>

56
gabmap/__init__.py Normal file
View File

@ -0,0 +1,56 @@
from fastkml import kml
from shapely.geometry import shape, Polygon, MultiPolygon, Point
__version__ = '0.0.1'
__author__ = 'Herbert Kruitbosch'
def as_gabmap_kml(geojson, document_name='Python Generated gabmap KML', name_property='gemeente_en_wijk_naam'):
"""Creates a string with kml, accepted by gabmap, based on the (multi)polygons and points in a GeoJSON.
If a feature in the geojson is a (Multi)Polygon, then both the (multi)polygon and the centroid point are
added to the KML output. `name_property` is used as the key in `'properties'` of the geojson to determine
the name used in the Placemarks of the KML. `document_name` is the name used in the Document element of the
KML."""
document = kml.Document(name=document_name, ns='')
illigal_symbols = set('"\'&')
intersection = set(document_name).intersection(illigal_symbols) == set()
assert intersection, (
"Gabmap cannot deal with some symbols ({}) in the document_name: {}".format(intersection, document_name))
names = set()
for feature in geojson['features']:
shape_ = shape(feature['geometry'])
name = feature['properties'][name_property]
assert isinstance(shape_, Polygon) or isinstance(shape_, MultiPolygon) or isinstance(shape_, Point), (
"gabmap.as_gabmap_kml was only tested on Polygons, Multipolygons and "
"Points, not on {}: ".format(type(shape_), name)
)
assert name not in names, "Gabmap cannot deal with duplicate names: {}".format(name)
intersection = set(name).intersection(illigal_symbols) == set()
assert intersection, (
"Gabmap cannot deal with some symbols ({}) in a name: {}".format(intersection, name))
if isinstance(shape_, Polygon) or isinstance(shape_, MultiPolygon):
names.add(name)
point_placemark = kml.Placemark(name=name, ns='')
point_placemark.geometry=shape_.centroid
document.append(point_placemark)
polygon_placemark = kml.Placemark(name=name, ns='')
polygon_placemark.geometry=shape_
document.append(polygon_placemark)
k = kml.KML()
k.append(document)
return (
"""<?xml version="1.0" encoding="utf-8" ?>
""" +
k.to_string(prettyprint=True)
# prettyprint is important, as gabmap requires the name-attributes of (multi)polygon-placemarkers to have a newline after the closing tag.
)

Binary file not shown.

28
setup.py Normal file
View File

@ -0,0 +1,28 @@
from setuptools import setup
setup(
name='gabmap',
version=__import__('gabmap').__version__,
url='https://www.gabmap.nl/',
author='Martijn Wieling, Herbert Kruitbosch, Research and Innovation Support',
author_email='H.T.Kruitbosch@rug.nl',
description='Python module with tools to prepare gabmap data. (Currently) Only supports conversion from geojson to gabmap-supported kml. Tested on Python3.6, probably also works in 2.7 and 3.x',
license='BSD',
packages=[
'gabmap',
],
include_package_data=True,
install_requires=[
'fastkml>=0.11,<2',
'shapely>=1.5.17,<2'
],
extras_require={},
zip_safe=True,
classifiers=[
'Intended Audience :: Developers',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Topic :: Software Development :: Libraries :: Python Modules',
],
)