From 8894e773e92a5a8b35695dd43596ed908fb1ae5c Mon Sep 17 00:00:00 2001 From: dengwirda Date: Sat, 30 Aug 2025 11:55:43 +1000 Subject: [PATCH] Import for geojson files containing a single feature --- jigsawpy/__init__.py | 2 +- jigsawpy/parse/loadgeo.py | 69 +++++++++++++++++++++++++++++---------- pyproject.toml | 2 +- setup.py | 2 +- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/jigsawpy/__init__.py b/jigsawpy/__init__.py index 574034a..d008eba 100644 --- a/jigsawpy/__init__.py +++ b/jigsawpy/__init__.py @@ -13,7 +13,7 @@ * JIGSAW: Interface to the JIGSAW meshing library. ------------------------------------------------------------ * - * Last updated: 13 Aug., 2025 + * Last updated: 30 Aug., 2025 * * Copyright 2019-2025 * Darren Engwirda diff --git a/jigsawpy/parse/loadgeo.py b/jigsawpy/parse/loadgeo.py index 5723ceb..9fc3d7b 100644 --- a/jigsawpy/parse/loadgeo.py +++ b/jigsawpy/parse/loadgeo.py @@ -1,9 +1,11 @@ +import warnings import numpy as np import json import argparse -from jigsawpy import jigsaw_msh_t, savemsh +from jigsawpy.msh_t import jigsaw_msh_t +from jigsawpy.savemsh import savemsh def linegeo(line, nset, eset, nobj, last): @@ -30,7 +32,13 @@ def linegeo(line, nset, eset, nobj, last): last = last + npts - temp.vert2["coord"] = line[+0::] + data = np.asarray(line) + if (data.shape[1] > 2): + warnings.warn("Z-COORD omitted", Warning) + + # omit z if [x,y,z] + temp.vert2["coord"][:, 0] = data[+0:, 0] + temp.vert2["coord"][:, 1] = data[+0:, 1] temp.edge2["index"][:, 0] = indx + 0 temp.edge2["index"][:, 1] = indx + 1 @@ -75,7 +83,13 @@ def polygeo(loop, nset, eset, nobj, last): last = last + npts - temp.vert2["coord"] = loop[:-1:] + data = np.asarray(loop) + if (data.shape[1] > 2): + warnings.warn("Z-COORD omitted", Warning) + + # omit z if [x,y,z] + temp.vert2["coord"][:, 0] = data[:-1, 0] + temp.vert2["coord"][:, 1] = data[:-1, 1] temp.edge2["index"][:, 0] = idx1 temp.edge2["index"][:, 1] = idx2 @@ -92,28 +106,28 @@ def readgeo(geom, nset, eset, nobj, last): """ - if (geom["type"] == "LineString"): + if (geom["type"].lower() == "linestring"): line = geom["coordinates"] nobj, last = linegeo( line, nset, eset, nobj, last) - elif (geom["type"] == "MultiLineString"): + elif (geom["type"].lower() == "multilinestring"): for line in geom["coordinates"]: nobj, last = linegeo( line, nset, eset, nobj, last) - elif (geom["type"] == "Polygon"): + elif (geom["type"].lower() == "polygon"): for loop in geom["coordinates"]: nobj, last = polygeo( loop, nset, eset, nobj, last) - elif (geom["type"] == "MultiPolygon"): + elif (geom["type"].lower() == "multipolygon"): for poly in geom["coordinates"]: for loop in poly: @@ -124,6 +138,29 @@ def readgeo(geom, nset, eset, nobj, last): return nobj, last +def readobj(feat, nset, eset, nobj, last): + """ + READOBJ: read a geoJSON feat into a jigsaw msh_t object. + + """ + + if (feat["type"].lower() != "geometrycollection"): + + geom = feat["geometry"] + + nobj, last = readgeo( + geom, nset, eset, nobj, last) + + else: + + for geom in feat["geometries"]: + + nobj, last = readgeo( + geom, nset, eset, nobj, last) + + return nobj, last + + def loadgeo(name, mesh): """ LOADGEO: load a geoJSON file into a jigsaw msh_t object. @@ -140,21 +177,17 @@ def loadgeo(name, mesh): with open(name) as f: geoj = json.load(f) - for feat in geoj["features"]: - - geom = (feat["geometry"]) - - if (geom["type"] != "GeometryCollection"): + if (geoj["type"].lower() != "featurecollection"): - nobj, last = readgeo( - geom, nset, eset, nobj, last) + nobj, last = readobj( + geoj, nset, eset, nobj, last) - else: + else: - for next in geom["geometries"]: + for feat in geoj["features"]: - nobj, last = readgeo( - next, nset, eset, nobj, last) + nobj, last = readobj( + feat, nset, eset, nobj, last) mesh.vert2 = np.concatenate(nset, axis=0) mesh.edge2 = np.concatenate(eset, axis=0) diff --git a/pyproject.toml b/pyproject.toml index bda84de..a98b7af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "jigsawpy" -version = "1.1.0" +version = "1.1.1" description = "Python interface for the JIGSAW meshing library." readme = "README.md" authors = [ diff --git a/setup.py b/setup.py index 0bc53fd..bb02a7e 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ AUTHOR = "Darren Engwirda" AUTHOR_EMAIL = "d.engwirda@gmail.com" URL = "https://github.com/dengwirda/" -VERSION = "1.1.0" +VERSION = "1.1.1" REQUIRES_PYTHON = ">=3.6.0" KEYWORDS = "Mesh-generation Delaunay Voronoi"