I'm working on openlayers 3 and ExtJS 6. I have a vector who is loaded with a GeoJson. I clear it and I want to add some new features once it's cleared.
My new data are provided by a store. So this is what I want to add in my vector :
[{"type":"Feature","properties":{"SCALERANK":6,"NATSCALE":30,"LABELRANK":5,"FEATURECLA":"Populated place","NAME":"Marsabit","NAMEPAR":null,"NAMEALT":null,"DIFFASCII":0,"NAMEASCII":"Marsabit","ADM0CAP":0,"CAPALT":0,"CAPIN":null,"WORLDCITY":0,"MEGACITY":0,"SOV0NAME":"Kenya","SOV_A3":"KEN","ADM0NAME":"Kenya","ADM0_A3":"KEN","ADM1NAME":"Eastern","ISO_A2":"KE","NOTE":null,"LATITUDE":2.329999,"LONGITUDE":37.979997,"CHANGED":1,"NAMEDIFF":0,"DIFFNOTE":"Changed scale rank.","POP_MAX":16460,"POP_MIN":15361,"POP_OTHER":16460,"RANK_MAX":6,"RANK_MIN":6,"GEONAMEID":187585,"MEGANAME":null,"LS_NAME":"Marsabit","LS_MATCH":1,"CHECKME":0,"MAX_POP10":16460,"MAX_POP20":16460,"MAX_POP50":0,"MAX_POP300":0,"MAX_POP310":0,"MAX_NATSCA":20,"MIN_AREAKM":12,"MAX_AREAKM":12,"MIN_AREAMI":5,"MAX_AREAMI":5,"MIN_PERKM":17,"MAX_PERKM":17,"MIN_PERMI":10,"MAX_PERMI":10,"MIN_BBXMIN":37.975,"MAX_BBXMIN":37.975,"MIN_BBXMAX":38.033333,"MAX_BBXMAX":38.033333,"MIN_BBYMIN":2.325,"MAX_BBYMIN":2.325,"MIN_BBYMAX":2.341667,"MAX_BBYMAX":2.341667,"MEAN_BBXC":38.004167,"MEAN_BBYC":2.333333,"COMPARE":0,"GN_ASCII":"Marsabit","FEATURE_CL":"P","FEATURE_CO":"PPL","ADMIN1_COD":3,"GN_POP":15361,"ELEVATION":0,"GTOPO30":1344,"TIMEZONE":"Africa/Nairobi","GEONAMESNO":"Geonames ascii name + lat.d + long.d matching.","UN_FID":0,"UN_ADM0":null,"UN_LAT":0,"UN_LONG":0,"POP1950":0,"POP1955":0,"POP1960":0,"POP1965":0,"POP1970":0,"POP1975":0,"POP1980":0,"POP1985":0,"POP1990":0,"POP1995":0,"POP2000":0,"POP2005":0,"POP2010":0,"POP2015":0,"POP2020":0,"POP2025":0,"POP2050":0,"CITYALT":null},"geometry":{"type":"Point","coordinates":[37.979996702838946,2.329998595077768]},"NAME":"Marsabit","id":"AIS_LIVE.model.PlacesModel-13153"},{"type":"Feature","properties":{"SCALERANK":7,"NATSCALE":20,"LABELRANK":3,"FEATURECLA":"Populated place","NAME":"Marsala","NAMEPAR":null,"NAMEALT":null,"DIFFASCII":0,"NAMEASCII":"Marsala","ADM0CAP":0,"CAPALT":0,"CAPIN":null,"WORLDCITY":0,"MEGACITY":0,"SOV0NAME":"Italy","SOV_A3":"ITA","ADM0NAME":"Italy","ADM0_A3":"ITA","ADM1NAME":"Sicily","ISO_A2":"IT","NOTE":null,"LATITUDE":37.805404,"LONGITUDE":12.438662,"CHANGED":0,"NAMEDIFF":0,"DIFFNOTE":null,"POP_MAX":77784,"POP_MIN":43179,"POP_OTHER":42587,"RANK_MAX":8,"RANK_MIN":7,"GEONAMEID":2524245,"MEGANAME":null,"LS_NAME":"Marsala","LS_MATCH":1,"CHECKME":0,"MAX_POP10":43179,"MAX_POP20":43179,"MAX_POP50":0,"MAX_POP300":0,"MAX_POP310":0,"MAX_NATSCA":20,"MIN_AREAKM":19,"MAX_AREAKM":19,"MIN_AREAMI":7,"MAX_AREAMI":7,"MIN_PERKM":25,"MAX_PERKM":25,"MIN_PERMI":16,"MAX_PERMI":16,"MIN_BBXMIN":12.425,"MAX_BBXMIN":12.425,"MIN_BBXMAX":12.491667,"MAX_BBXMAX":12.491667,"MIN_BBYMIN":37.766667,"MAX_BBYMIN":37.766667,"MIN_BBYMAX":37.816667,"MAX_BBYMAX":37.816667,"MEAN_BBXC":12.458631,"MEAN_BBYC":37.794643,"COMPARE":0,"GN_ASCII":"Marsala","FEATURE_CL":"P","FEATURE_CO":"PPL","ADMIN1_COD":15,"GN_POP":77784,"ELEVATION":0,"GTOPO30":7,"TIMEZONE":"Europe/Rome","GEONAMESNO":"Geonames ascii name + lat.d + long.d matching.","UN_FID":0,"UN_ADM0":null,"UN_LAT":0,"UN_LONG":0,"POP1950":0,"POP1955":0,"POP1960":0,"POP1965":0,"POP1970":0,"POP1975":0,"POP1980":0,"POP1985":0,"POP1990":0,"POP1995":0,"POP2000":0,"POP2005":0,"POP2010":0,"POP2015":0,"POP2020":0,"POP2025":0,"POP2050":0,"CITYALT":null},"geometry":{"type":"Point","coordinates":[12.43866166041903,37.805404275558374]},"NAME":"Marsala","id":"AIS_LIVE.model.PlacesModel-10722"},{"type":"Feature","properties":{"SCALERANK":4,"NATSCALE":50,"LABELRANK":3,"FEATURECLA":"Admin-1 region capital","NAME":"Marseille","NAMEPAR":null,"NAMEALT":"Marseille-Aix-en-Provence","DIFFASCII":0,"NAMEASCII":"Marseille","ADM0CAP":0,"CAPALT":0,"CAPIN":null,"WORLDCITY":0,"MEGACITY":1,"SOV0NAME":"French Republic","SOV_A3":"FRA","ADM0NAME":"France","ADM0_A3":"FRA","ADM1NAME":"Provence-Alpes-C-te-d'Azur","ISO_A2":"FR","NOTE":null,"LATITUDE":43.289979,"LONGITUDE":5.37501,"CHANGED":4,"NAMEDIFF":0,"DIFFNOTE":"Changed feature class.","POP_MAX":1400000,"POP_MIN":794811,"POP_OTHER":813666,"RANK_MAX":12,"RANK_MIN":11,"GEONAMEID":2995469,"MEGANAME":"Marseille-Aix-en-Provence","LS_NAME":"Marseille","LS_MATCH":1,"CHECKME":0,"MAX_POP10":946129,"MAX_POP20":1034294,"MAX_POP50":1034294,"MAX_POP300":0,"MAX_POP310":0,"MAX_NATSCA":50,"MIN_AREAKM":285,"MAX_AREAKM":361,"MIN_AREAMI":110,"MAX_AREAMI":140,"MIN_PERKM":237,"MAX_PERKM":327,"MIN_PERMI":147,"MAX_PERMI":203,"MIN_BBXMIN":5.158333,"MAX_BBXMIN":5.183333,"MIN_BBXMAX":5.633333,"MAX_BBXMAX":5.633333,"MIN_BBYMIN":43.216667,"MAX_BBYMIN":43.216667,"MIN_BBYMAX":43.466123,"MAX_BBYMAX":43.525,"MEAN_BBXC":5.397504,"MEAN_BBYC":43.351337,"COMPARE":0,"GN_ASCII":"Marseille","FEATURE_CL":"P","FEATURE_CO":"PPLA","ADMIN1_COD":0,"GN_POP":794811,"ELEVATION":0,"GTOPO30":54,"TIMEZONE":"Europe/Paris","GEONAMESNO":"Geonames ascii name + lat.d + long.d matching.","UN_FID":187,"UN_ADM0":"France","UN_LAT":43.28,"UN_LONG":5.38,"POP1950":756,"POP1955":798,"POP1960":929,"POP1965":1069,"POP1970":1182,"POP1975":1253,"POP1980":1295,"POP1985":1307,"POP1990":1305,"POP1995":1331,"POP2000":1357,"POP2005":1386,"POP2010":1400,"POP2015":1418,"POP2020":1445,"POP2025":1469,"POP2050":1490,"CITYALT":"Marseille"},"geometry":{"type":"Point","coordinates":[5.37306427182989,43.29192492260455]},"NAME":"Marseille","id":"AIS_LIVE.model.PlacesModel-13925"}]
But if I execute: me.getData('myVector').getSource().addFeatures(myData)
I've got an error.
I also tried
var format = new ol.format.GeoJSON();
me.getData('myVector').addFeatures(format.readFeatures( myData, {
featureProjection: 'EPSG:3857'
} ) );
It gives me 0 error but I when I want to display all my features there is nothing in my vector.
How can I do?
Going through the second part of Nimrod's tutorial I've reached the part were macros are explained. The documentation says they run at compile time, so I thought I could do some parsing of strings to create myself a domain specific language. However, there are no examples of how to do this, the debug macro example doesn't display how one deals with a string parameter.
I want to convert code like:
instantiate("""
height,f,132.4
weight,f,75.0
age,i,25
""")
…into something which by hand I would write like:
var height: float = 132.4
var weight: float = 75.0
var age: int = 25
Obviously this example is not very useful, but I want to look at something simple (multiline/comma splitting, then transformation) which could help me implement something more complex.
My issue here is how does the macro obtain the input string, parse it (at compile time!), and what kind of code can run at compile time (is it just a subset of a languaje? can I use macros/code from other imported modules)?
EDIT: Based on the answer here's a possible code solution to the question:
import macros, strutils
# Helper proc, macro inline lambdas don't seem to compile.
proc cleaner(x: var string) = x = x.strip()
macro declare(s: string): stmt =
# First split all the input into separate lines.
var
rawLines = split(s.strVal, {char(0x0A), char(0x0D)})
buf = ""
for rawLine in rawLines:
# Split the input line into three columns, stripped, and parse.
var chunks = split(rawLine, ',')
map(chunks, cleaner)
if chunks.len != 3:
error("Declare macro syntax is 3 comma separated values:\n" &
"Got: '" & rawLine & "'")
# Add the statement, preppending a block if the buffer is empty.
if buf.len < 1: buf = "var\n"
buf &= " " & chunks[0] & ": "
# Parse the input type, which is an abbreviation.
case chunks[1]
of "i": buf &= "int = "
of "f": buf &= "float = "
else: error("Unexpected type '" & chunks[1] & "'")
buf &= chunks[2] & "\n"
# Finally, check if we did add any variable!
if buf.len > 0:
result = parseStmt(buf)
else:
error("Didn't find any input values!")
declare("""
x, i, 314
y, f, 3.14
""")
echo x
echo y
Macros can, by and large, utilize all pure Nimrod code that a procedure in the same place could see, too. E.g., you can import strutils or peg to parse your string, then construct output from that. Example:
import macros, strutils
macro declare(s: string): stmt =
var parts = split(s.strVal, {' ', ','})
if len(parts) != 3:
error("declare macro requires three parts")
result = parseStmt("var $1: $2 = $3" % parts)
declare("x, int, 314")
echo x
"Calling" a macro will basically evaluate it at compile time as though it were a procedure (with the caveat that the macro arguments will actually be ASTs, hence the need to use s.strVal above instead of s), then insert the AST that it returns at the position of the macro call.
The macro code is evaluated by the compiler's internal virtual machine.