d3.geoPath() returns Error: <path> attribute d: Expected number, "M,ZM,ZM,ZM,Z" - geojson

Edit: recreated the logic on jsfiddle https://jsfiddle.net/exLtcgrq/1/
I am trying to parse a simple GeoJSON file to D3 using the D3 V4 API.
My GeoJSON is simple:
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[10.0, 10.0], [60.0, 40.0], [50.0, 75.0],[20.0, 60.0]
]
},
"properties": {
"id": "1",
"Type": "campingspot"
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[20.0, 65.0], [50.0, 80.0], [50.0, 110.0],[20.0, 115.0]
]
},
"properties": {
"id": "1",
"Type": "campingspot"
}
}
]
}
I load thus using the d3.json() method and try using the d3-geo api to convert it to a path with this code:
var jsonData2 = d3.json("campingGeojson.json", function(error, json){
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", d3.geoPath())
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("fill", "green")
});
The console output on chrome tells me the following
Error: <path> attribute d: Expected number, "M,ZM,ZM,ZM,Z".
Any suggestions what is going wrong with using the geoPath method is highly appreciated.
Thank you.

Coordinates for geoJson polygons are an array of coordinate arrays (with the coordinates themselves being arrays). The first array indicates the shell, following arrays indicate holes.
So I think your geoJson should look more like:
"coordinates": [
[ [10.0, 10.0], [60.0, 40.0], [50.0, 75.0],[20.0, 60.0] ]
]

Related

convert grib to geojson

I want to convert a grib2 file to a geojson with the following format:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "ID": 0, "sigwaveht": 1.000000 }, "geometry": { "type": "LineString", "coordinates": [ [ 20.5, 77.559374979743737 ], [ 20.756756711040964, 77.5 ], [ 21.0, 77.426829270065582 ], [ 21.5, 77.426829270065582 ] ] } },
{ "type": "Feature", "properties": { "ID": 1, "sigwaveht": 1.000000 }, "geometry": { "type": "LineString", "coordinates": [ [ 17.5, 76.879518074163784 ], [ 18.0, 76.840000001907356 ], [ 18.555555592348554, 77.0 ], [ 18.555555592348554, 77.5 ] ] } },
{ "type": "Feature", "properties": { "ID": 2, "sigwaveht": 1.000000 }, "geometry": { "type": "LineString", "coordinates": [ [ 28.5, 76.732142838136269 ], [ 29.0, 76.634146323734484 ], [ 29.937500058207661, 77.0 ], [ 29.937500058207661, 77.5 ] ] } },
I can accomplish this by using ogr2ogr2 to convert a shape file to a geojson in this format but what can I do to convert a grib2 to a geoJSON of this format?
You can't convert a GRIB, which is a raster format, to GeoJSON, which is a vector format.
What do you expect to achieve? Vector data composed of points where each point is one of the pixels of the raster format?
If this is what you want, you will probably have to code it yourself, I don't think there are any standard tools to do this. Just make a loop over the raster data pixels and write one point feature for every pixel.

Change coordinates precision while serializing geodjano objects

I have a GeoDjango polygon model as below
class PolygonFeature(models.Model):
shapefile = models.ForeignKey(Shapefile,
on_delete=models.CASCADE, related_name='polygon_shp')
geom = models.PolygonField(srid=4326, blank=True, null=True)
def __str__(self):
return str(self.shapefile.filename) + "-" + str(self.pk)
I am adding polygon to model via LayerMapping in which out is the shapefile
mapping = {
'shapefile':{'id':'survey_pk'},
'geom': geom,
}
lm = LayerMapping(model, out, mapping, transform=transform, encoding='iso-8859-1')
lm.save(verbose=False)
I need to change coordinates precision while serializing polygon object geometry to GeoJSOn
feat = PolygonFeature.objects.filter(shapefile=obj)
json_obj = serialize('geojson', feat)
Current
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"name": "EPSG:4326"
},
"features": [
{
"type": "Feature",
"properties": {
"shapefile": 53,
"pk": "45269"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.6083423241,
31.315054275
],
[
75.5543398801,
31.30543373742
],
[
75.5970812345,
31.2980635331
],
[
75.6543504465,
31.30345354185
],
[
75.6035348241,
31.315035375
]
]
]
}
}
]
}

apiconnect: Create API for geoJSON data

Can someone explain how to create an API with APIC toolkit?
I would like to use this API to work with a Cloudant DB on IBM Bluemix or a local CouchDB to create, read and update of the geoJSON data.
Below is an easy example of typical data to store name and coordinates of point of interests.
[{
"type": "Feature",
"properties": {
"name": "Nice Place 1"
},
"geometry": {
"type": "Point",
"coordinates": [16.45961, 48.23896]
}
}, {
"type": "Feature",
"properties": {
"name": "Nice Place 2"
},
"geometry": {
"type": "Point",
"coordinates": [16.34561, 49.89612]
}
}]
LoopBack supports GeoPoint (i.e. Point in GeoJSON) datatype.
Considering your typical example, let's say you have a model named: Feature, then to use GeoPoint, your Feature.json should look like:
{
"name": "Feature",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string"
},
"geometry": {
"type": "geopoint"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Now, this Feature model, having PersistedModel as base, will have common CRUD methods exposed as REST endpoints and you can store data, for example, using CURL:
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
\"name\": \"Nice Place 1\",
\"geometry\": {
\"lat\": 16.20,
\"lng\": 48.23
}
}" "http://0.0.0.0:3000/api/Features"
Hope that helps with creating an API that supports GeoPoint.
Re: Cloudant db, I am not sure if it supports geo-spatial data out of the box, however there seems support for it: https://cloudant.com/product/cloudant-features/geospatial/
I tried with the model above with a loopback app(using cloudant as ds) and it's explorer:
Create with sample data:
{
"name": "string",
"geometry": {
"lat": 12,
"lng": 13
}
}
And get it from GET/ myGeoModels successfully:
[
{
"name": "string",
"geometry": {
"lat": 12,
"lng": 13
},
"id": "f08301abe833ad427c9c61ffd30df8ef"
}
]
APIC should have same behaviour of loopback.

I get differents geojson using GDAL ogr2ogr 1.10.1 or 1.9.0 version

I have two Ubuntu environments: one has installed GDAL 1.10.1 and second has installed GDAL 1.9.0.
If I run into env. GDAL 1.9.0: ogr2ogr -f GeoJSON filename.geojson filename.kml
filename.geojson has this structure:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "Name": "TV01-CTRA. ALMERÍA", "description": "\n \n
</div>
\n Ver detalle cámara</a>
\n \n ", "timestamp": null, "begin": null, "end": null, "altitudeMode": null, "tessellate": -1, "extrude": -1, "visibility": -1, "drawOrder": null, "icon": null },
"geometry": { "type": "Point", "coordinates": [ -4.35349321365356, 36.720241546630902 ] } },
but
If I run same command into GDAL 1.10.1: ogr2ogr -f GeoJSON filename.geojson filename.kml
filename.geojson has this structure:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "Name": "TV01-CTRA. ALMERÍA", "Description":
"</div>
Ver detalle cámara</a>" },
"geometry": { "type": "Point", "coordinates": [ -4.35349321365356, 36.720241546630902 ] } }
,
There are a lots of differences for me (\n, tabs, blank space, crs line,...), and I need to generate second version, that is, I need generate second structure, but I have to run command into GDAL 1.10.1 version.
Which parameters I have to add?
Thanks!!!

Problems extracting values from geojson

I'm struggling to create an array of the values for D01. This is the first feature in my geojson variable:
county = [{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::4269" } },
"features": [
{ "type": "Feature", "properties": { "NAMELSAD10": "St. Clair County", "D01": 5.650500, "D02": 0.504000, "D03": 0.495000, "D04": 48.100000, "D05": 0.199000, "D06": 0.965000, "D07": 0.038000, "D08": 0.031000, "D09": 0.211000 }, "geometry": { "type": "Polygon", "coordinates": [
//so many coordinate pairs
] } } ] ]
To clarify, I want [5.650500, ...]
From this post I used:
help = county.features.map(function(f){
return f.properties.D01;
})
which gives the error: Cannot read property "map" of undefined.
Thinking some of my values might be null, I wrote this:
var help = new Array();
try{
help = county.features.map(function(f){
return f.properties.D01;
})
}catch(e){}
console.log(help);
which results in [], the blank 'help' array being written.
It seems I'm not accessing what I want to here. Can someone please point me in the right direction?
Your county seems an Array.
try this:
county[0].features.map(function(f){
return f.properties.D01;
})

Resources