Skip to content

Commit

Permalink
exporter: use shape UUID as primary key, clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Mosler committed Nov 29, 2022
1 parent f800e34 commit ab1a1d0
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 136 deletions.
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,27 @@ setup: setup-map setup-exporter

image-exporter:
cd exporter && \
podman build -t wlw-partner-exporter .
docker build -t wlw-partner-exporter .

image-map:
cd map && \
podman build -t wlw-partner-map .
docker build -t wlw-partner-map .

images: image-map image-exporter

run-map:
cd map && npm start

run-map-container:
docker run --rm \
--mount type=bind,source=./exporter/geojson,target=/usr/share/nginx/html/geojson,ro,relabel=shared \
-p '8080:80' wlw-partner-map:latest

run-exporter:
cd exporter && ./run.sh

run-exporter-container:
docker run --rm \
--mount type=bind,source=./exporter/geojson,target=/opt/wlw-geojson-exporter/geojson,relabel=shared \
--mount type=bind,source=./exporter/db,target=/opt/wlw-geojson-exporter/db,ro,relabel=shared \
wlw-partner-exporter:latest
1 change: 1 addition & 0 deletions exporter/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ RUN apt-get update && apt-get install -y curl unzip
WORKDIR /opt/wlw-geojson-exporter

VOLUME ./geojson
VOLUME ./db

ADD . .

Expand Down
2 changes: 1 addition & 1 deletion exporter/get-shapes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set -o errexit
curl https://data.geo.admin.ch/ch.swisstopo.swissboundaries3d/swissboundaries3d_2022-05/swissboundaries3d_2022-05_2056_5728.shp.zip > swissboundaries3d.zip
mkdir -p shapes
unzip -u swissboundaries3d.zip -d shapes
rm swissboundaries3d.zip
rm swissboundaries3d.zip
25 changes: 16 additions & 9 deletions exporter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@dataclass
class Partner:
zip_code: int
UUID: int
status: str
url: str
comment: str
Expand All @@ -39,7 +39,7 @@ class Partner:
def find_partner(zip_code: int) -> Union[Partner, None]:
with con:
cursor = con.execute(
"SELECT zip_code, status, url, comment FROM partners WHERE zip_code = ?",
"SELECT UUID, status, url, comment FROM partners WHERE UUID = ?",
(zip_code, ))

result = cursor.fetchone()
Expand All @@ -48,16 +48,26 @@ def find_partner(zip_code: int) -> Union[Partner, None]:


def simplify(points: List[tuple[float, float]]) -> List[tuple[float, float]]:
'''Reduce number of points in polygon using https://shapely.readthedocs.io/en/stable/manual.html?highlight=simplify#object.simplify'''
return [
p for p in Polygon(np.array(points)).simplify(
0.0025, preserve_topology=True).exterior.coords
]


def transform(points: List[tuple[float, float]]) -> List[tuple[float, float]]:
'''Convert coordinates from LV95 to WGS84 using https://pyproj4.github.io/pyproj/dev/examples.html#step-2-create-transformer-to-convert-from-crs-to-crs'''
return [transformer.transform(x, y) for x, y in points]


def get_geojson(shapeRecord: shapefile.ShapeRecords):
geojson = shapeRecord.__geo_interface__
del geojson['properties']['UUID'] # reduce file size ~0.1MB
new_coords = simplify(transform(geojson['geometry']['coordinates'][0]))
geojson['geometry']['coordinates'][0] = new_coords
return geojson


logger.info("reading shapes")

# schema
Expand All @@ -74,17 +84,14 @@ def transform(points: List[tuple[float, float]]) -> List[tuple[float, float]]:
sf = shapefile.Reader(
'shapes/SHAPEFILE_LV95_LN02/swissBOUNDARIES3D_1_3_TLM_HOHEITSGEBIET')

fields = ('NAME', 'BFS_NUMMER')
fields = ('UUID', 'NAME')
collection = {'type': 'FeatureCollection', 'features': []}

logger.info("transform and merge")

for shape in sf.shapeRecords(fields=fields):
geojson = shape.__geo_interface__
new_coords = simplify(transform(geojson['geometry']['coordinates'][0]))
geojson['geometry']['coordinates'][0] = new_coords

partner = find_partner(geojson['properties']['BFS_NUMMER'])
for shapeRecord in sf.shapeRecords(fields=fields):
geojson = get_geojson(shapeRecord)
partner = find_partner(shapeRecord.record.UUID)

if partner:
geojson['properties']['status'] = partner.status
Expand Down
5 changes: 3 additions & 2 deletions exporter/setup-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mkdir -p db

if [ ! -f db/partners.db ]; then
sqlite3 db/partners.db 'CREATE TABLE "partners" (
"UUID" TEXT NOT NULL,
"zip_code" INTEGER NOT NULL,
"status" TEXT CHECK("status" IN (NULL, "P", "K", "I")),
"url" TEXT,
Expand All @@ -13,6 +14,6 @@ if [ ! -f db/partners.db ]; then
"partnership_started_at" TEXT,
"partnership_cancelled_at" TEXT,
"updated_at" INTEGER,
PRIMARY KEY("zip_code")
PRIMARY KEY("UUID")
)'
fi;
fi;
4 changes: 2 additions & 2 deletions map/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nodejs:18-alpine as build
FROM node:18-alpine as build

WORKDIR /opt/wlw-partner-map

Expand All @@ -10,4 +10,4 @@ FROM nginx:alpine

COPY --from=build /opt/wlw-partner-map /usr/share/nginx/html

VOLUME ./usr/share/nginx/html/geojson
VOLUME ./geojson
6 changes: 6 additions & 0 deletions map/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
integrity="sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI="
crossorigin=""
/>
<script
src="https://unpkg.com/leaflet@1.9.3/dist/leaflet.js"
integrity="sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM="
crossorigin=""
></script>
<style>
#map {
width: 100%;
Expand All @@ -21,6 +26,7 @@
<script type="module" src="index.js"></script>
</head>
<body>
<div id="error"></div>
<div id="map"></div>
</body>
</html>
25 changes: 13 additions & 12 deletions map/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import 'leaflet'
const response = await fetch('/geojson/partners.geojson')

const resp = await fetch('/geojson/partners.geojson')
const data = await resp.json()
if (!response.ok) {
document.querySelector('#error').textContent = 'Could not load GeoJSON data'
}

const data = await response.json()

const merenschwandCenter = [47.25941, 8.37492]
const initialZoomLevel = 10
const map = L.map('map').setView(merenschwandCenter, initialZoomLevel)

const renderPopup = layer => {
const { NAME, url, comment, BFS_NUMMER } = layer.feature.properties
return `${NAME} ${BFS_NUMMER}
const { NAME, url, comment } = layer.feature.properties
return `${NAME}
${url ? `<br/><a target="_blank" href="${url}">${url}</a>` : ''}
${comment ? `<br/>${comment}` : ''}`
}

const style = feature => {
const color =
feature.properties.status === 'P'
? 'green'
: feature.properties.status === 'I'
? 'red'
: '#BBB'
return { color }
const { status } = feature.properties
const color = status === 'P' ? 'green' : status === 'I' ? 'red' : '#BBB'

const opacity = status === 'P' ? 1 : 0.5
return { color, opacity }
}

L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
Expand Down
Loading

0 comments on commit ab1a1d0

Please sign in to comment.