Skip to main content
Solved

How do I use Xquery to create a JSON linestring?


My goal is to create a JSON file that matches the following template. I've been struggling getting the coordinates in the linestring geometry property properly formatted. Note the addition of the "tippecanoe" property.

{
    "type" : "Feature",
    "tippecanoe" : { "maxzoom" : 9, "minzoom" : 4 },
    "properties" : { "FULLNAME" : "N Vasco Rd" },
    "geometry" : {
        "type" : "LineString",
        "coordinates" : [ [ -121.73335037.767671 ], [ -121.73360037.767483 ], [ -121.73313137.766952 ] ]
    }
}

Things I tried:

  • Basic geojson writer - Writes the maxoom/minzoom property under "properties" instead of "tippecanoe".
  • JSONTemplater - 
concat('[[',geom:get-points('xy'', ''],['),']]')

This method works, but feels hackish; it adds quotation marks (") which I remove later. 

geom:get-text-data(geom:get-name())

The second method returns null values. I give the geometry a name with the GeometryPropertySetter, but still returns a null value. If someone can post a working example, I can go from there. The documentation is little light. Thank you!

Best answer by takashi

Hi @justinmatis, you can use the GeometryExtractor (GeometryEncoding: GeoJSON) to translate the feature geometry to a geometry string '{"type": <geometry type>, "coordinates": <coordinates array>}' in the GeoJSON format and store it in a feature attribute.
Assuming the geometry string is stored in an attribute named "_geometry", this template expression should generate your desired JSON document.

{
    "type" : "Feature",
    "tippecanoe" : {
        "maxzoom" : fme:get-attribute("maxzooom"),
        "minzoom" : fme:get-attribute("minzoom")
    },
    "properties" : {
        "FULLNAME" : fme:get-attribute("FULLNAME")
    },
    "geometry" : fme:get-json-attribute("_geometry")
}
View original
Did this help you find an answer to your question?

4 replies

takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • Best Answer
  • August 12, 2018

Hi @justinmatis, you can use the GeometryExtractor (GeometryEncoding: GeoJSON) to translate the feature geometry to a geometry string '{"type": <geometry type>, "coordinates": <coordinates array>}' in the GeoJSON format and store it in a feature attribute.
Assuming the geometry string is stored in an attribute named "_geometry", this template expression should generate your desired JSON document.

{
    "type" : "Feature",
    "tippecanoe" : {
        "maxzoom" : fme:get-attribute("maxzooom"),
        "minzoom" : fme:get-attribute("minzoom")
    },
    "properties" : {
        "FULLNAME" : fme:get-attribute("FULLNAME")
    },
    "geometry" : fme:get-json-attribute("_geometry")
}

takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • August 12, 2018
takashi wrote:

Hi @justinmatis, you can use the GeometryExtractor (GeometryEncoding: GeoJSON) to translate the feature geometry to a geometry string '{"type": <geometry type>, "coordinates": <coordinates array>}' in the GeoJSON format and store it in a feature attribute.
Assuming the geometry string is stored in an attribute named "_geometry", this template expression should generate your desired JSON document.

{
    "type" : "Feature",
    "tippecanoe" : {
        "maxzoom" : fme:get-attribute("maxzooom"),
        "minzoom" : fme:get-attribute("minzoom")
    },
    "properties" : {
        "FULLNAME" : fme:get-attribute("FULLNAME")
    },
    "geometry" : fme:get-json-attribute("_geometry")
}
I'd recommend you to use the GeometryExtractor as I suggested above, but if it cannot be used for some reason, this expression would be available instead.

 

{
    "type" : "Feature",
    "tippecanoe" : {
        "maxzoom" : fme:get-attribute("maxzooom"),
        "minzoom" : fme:get-attribute("minzoom")
    },
    "properties" : {
        "FULLNAME" : fme:get-attribute("FULLNAME")
    },
    "geometry" : {
        "type" : "LineString",
        "coordinates" : [
            for $coord in tokenize(geom:get-points("xy"), '\s')
            return [
                for $v in tokenize($coord, ',')
                return xs:double($v)
            ]
        ]
    }

takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • August 12, 2018
takashi wrote:

Hi @justinmatis, you can use the GeometryExtractor (GeometryEncoding: GeoJSON) to translate the feature geometry to a geometry string '{"type": <geometry type>, "coordinates": <coordinates array>}' in the GeoJSON format and store it in a feature attribute.
Assuming the geometry string is stored in an attribute named "_geometry", this template expression should generate your desired JSON document.

{
    "type" : "Feature",
    "tippecanoe" : {
        "maxzoom" : fme:get-attribute("maxzooom"),
        "minzoom" : fme:get-attribute("minzoom")
    },
    "properties" : {
        "FULLNAME" : fme:get-attribute("FULLNAME")
    },
    "geometry" : fme:get-json-attribute("_geometry")
}
And, if you need to get geometry name in the expression, just call the "geom:get-name" function. as in:

 

{
    ....
    "properties" : {
        "FULLNAME" : fme:get-attribute("FULLNAME"),
        "GeometryName": geom:get-name()
    },
    ...
}

takashi wrote:

Hi @justinmatis, you can use the GeometryExtractor (GeometryEncoding: GeoJSON) to translate the feature geometry to a geometry string '{"type": <geometry type>, "coordinates": <coordinates array>}' in the GeoJSON format and store it in a feature attribute.
Assuming the geometry string is stored in an attribute named "_geometry", this template expression should generate your desired JSON document.

{
    "type" : "Feature",
    "tippecanoe" : {
        "maxzoom" : fme:get-attribute("maxzooom"),
        "minzoom" : fme:get-attribute("minzoom")
    },
    "properties" : {
        "FULLNAME" : fme:get-attribute("FULLNAME")
    },
    "geometry" : fme:get-json-attribute("_geometry")
}

 

Great answer. I noticed this converted my linestring to xyz data where I had no z data. Threw in a 2DForcer to correct this.

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings