How to get parent administrative boundaries via OverPass API - openstreetmap

Having an administrative boundary relation of certain level, what is the best way to get parent level boundaries?
This works for Bratislava city part Petržalka:
http://overpass-turbo.eu/s/ewU
[out:csv(::id,::type,"name","admin_level")];
rel(2208781);
(._;<<;);
out;
The result is:
#id #type name admin_level
14296 relation Slovensko 2
388210 relation okres Bratislava V 8
388265 relation Bratislavský kraj 4
1702499 relation Bratislava 6
2208781 relation Petržalka 9
But it doesn't work for Vienna part Meidling
http://overpass-turbo.eu/s/ex1
[out:csv(::id,::type,"name","admin_level")];
rel(1990596);
(._;<<;);
out;
the result
#id #type name admin_level
1990596 relation Meidling 9
Obviously, the problem is that there are no sub-relations defined in Vienna boundary relations. Is there any reliable way to find these parent-child relations?

Related

Convert Relational Database model to AGE database

I have made an ER diagram of course management system considering Relational Database.
Here is the picture:
[Course Management System] (https://i.stack.imgur.com/RBNfg.png)
How can I map these things in Apache AGE?
What will be the Vertices and what will be the Edges in this case?
I have ER diagram of Course Management System considering Relational Database Model. I want guidance in converting it to Apache AGE Database Model.
I can give you an idea to a certain extent.
Create a Graph with some name(e.g. Registration_Details)
The nodes/vertices will be Person.
Since there are two types of Person, you can use vertex label names to distinguish between them(label name as Student and Teacher and).
Properties of vertex with label name Student will be {'Student_ID': , 'Address': , 'CGPA': } and similarly for vertex with label name Teacher.
Now for Registration details, You'll be creating all possible edges between Student and Teacher. For every edge created you'll be adding properties of edges as well!
example
StudentA(vertex-{StudID: ,...}) ------- edge1({'RegId':123, 'CourseId':789, 'Marks': 80}) ------ TeacherA(Vertex)
I have not included StudentID and TeacherID in edge1 because in Apache AGE the edge already consists of information about the vertexIDs.
Hope this gives some head start for you.
As a rule of thumb, table names in relational databases become label names for vertices, table columns become the properties of those vertices, and the relationships between tables (defined by foreign keys in relational databases) become the edges between vertices.
However, while you could technically map relational databases 1:1 onto graph databases, this may not be the best approach because graphs can offer us more powerful design choices. Check out this blog post for more info:
https://maxdemarzi.com/2015/08/26/modeling-airline-flights-in-neo4j/
You should first write down the relationship between the tables as each table becomes a vertex and it has some properties which are the columns. Then each vertex(TABLE) is connected to another vertex(TABLE) through an edge(RELATIONSHIP). This edge would also have some properties depending upon how you want to utilize the data. You can take help from the here
You need to create a vertex type for each entity (table), and then define the edges that connect the vertices based on the relationships in the ER diagram. You will also need to define attributes for edges along with defining attributes for vertexes as needed.
In your ERD you have defined your tables now you just have to convert those tables into vertexes and the relationships between them would become edges between those vertexes. This means your tables here represent your vertex and your edges represent the relationship between those tables.

How to query many to many relationship?

So I got many to many relationship in two different entities. One has a list of the other and the other has a list of the first one. My question is how to make query that takes one field of the entity + the collection. For example Professors and Projects are related many to many, how to get the name of all professors + the projects they are involved in? Please if you have ideas share deeply and broadly. Thanks
You can use JP QL Projections, select on the Professor's name plus the properties of the Project entity associated with the professor. Using your example:
#Entity
public class Professor {
private Long id;
private String name;
private Collection<Projects> projects;
...
}
#Entity
public class Project {
private Long id;
private String status;
private Collection<Professor> professors;
...
}
Sample data:
Your Professor table has 2 rows:
1 Mark
2 John
Your Project table has 4 rows:
1 A DELAYED
2 B COMPLETED
3 C ON-TRACK
4 D COMPLETED
The JOIN table (the first column is the Professor ID, 2nd column is the Project ID):
1 1
1 2
2 3
2 4
Your JPA Query will be like:
SELECT prof.name, proj.name, proj.status FROM Professor prof JOIN prof.projects proj
Then the results will be:
Mark A DELAYED
Mark B COMPLETED
John C ON-TRACK
John D COMPLETED
However, if you're trying use projection on the Professor name plus its project collections, that will not be possible in JP QL. JP QL only allows single-valued path expression in the SELECT clause.
Example: This is NOT A VALID JP QL Query, since prof.projects is a collection-valued path expression.
SELECT prof.name, prof.projects FROM Professor prof

How to expose extra column data for each pairing on a many to many relationship?

I have a Cars table, a BodyPaints table, and a Cars_BodyPaints association table that defines what paint jobs are possible for various cars. The association between Cars and BodyPaints is many to many.
I'd like to create an extra piece of data for each (Car, Paint) mapping to define the cost of a specific paint job on a specific car. I have added an extra column to Cars_BodyPaints to record the price of each pairing, but when I update the model classes from the DB I don't see it exposed in any of the entity classes. This leads me to believe that maybe my approach is wrong here. I was excepting some method to be generated so I could execute code like:
Car civic = (from c in context.Cars where c.Name == "Civic" select c).Single();
Paint red = (from p in civic.Paints where p.Name == "Red" select p).Single();
var price = civic.GetPrice(red);
Am I off base here? How would you accomplish this?
Thanks
When modeling many-to-many relationships with attributes, you will need to model the relation as an entity itself. You would create a new entity for the Cars_BodyPaints table and it would contain your extra price column as a property. This new intermediary entity must be traversed when considering the relation between Cars and BodyPaints and can also be directly queried.
Cars <-- Cars_BodyPaints --> BodyPaints
In this new model, your query would look more like:
(from bp in context.CarBodyPaints
where bp.Car.Name = "Civic" and bp.Paint.Name = "Red"
select bp.Price).Single()

Entity Framework Endpoint Multiplicity

Say I have 3 tables in a Tennis Application (stripped down removing irrelevant info):
Competitions
Id (PK)
Matches
Id (PK)
CompId (FK)
CourtAssignments
CompId (PK),(FK)
CourtNumber (PK)
MatchId (FK), (Unique)
To describe the above:
A match consists of 2 people playing tennis against eachother on a court.
A competition consists of 0 to many matches.
A courtassignment represents a court during a competition (During a single competition, 0 or 1 match may be assigned to a court) Also, a single match can only be played on a single court number, and exists only in a single competition. (so those two fields together form the primary key for the CourtAssignment's table)
Thus the CourtAssignment's MatchId field will ALWAYS be unique or null.
However, when a generate an EF Model from my database. The multiplicity for my CourtAssignment Navigation Property with my Match is *. This should be 0..1.
I am using Visual Studio 2010 beta 2 (with .Net 4 beta 2 and EF 4 beta 2).
I had been using beta 1 and was able to simply change the * to 0..1. However, now having changed to beta 2 since it has a go-live license (porting upgrading my solution worked fine, but re-writing my solution from scratch, I am unable to manuall change * to 0..1 without receiving an error:
Error 113: Multiplicity is not valid in Role 'CourtAssignments' in relationship 'CourtAssignments_MatchId_FK_Matches_Id'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
Question
How can I change the multiplicity to 0..1? I know it is doable since my old solution continues to work that way. I can't seem to make the change in the designer anymore and get completely lost in the edmx file.
Note: I realize changing MatchId to the primary key would generate the correct multiplicity, but I need my primary key to be the composite of [CompId,CourtNumber] as I need to be able to switch which match is on which court on the fly. And obviously it doesn't work changing the primary key.
Thank you to whoever can provide help!
Scott, this isn't the exactl same scenario as you, but it might help:
Unique Keys not recognized by Entity Framework
I think it may be related - perhaps you've got the FK cols exposed in the conceptual model? It seems you can't do a 1 to 0..1 if this is the case.

How do I express a polymorphic association in JPA?

A polymorphic association is similar to a foreign key or many-to-one relationship, with the difference being that the target might be one of a number of types (classes in the language, tables in the db).
I'm porting a database design I've been using for some years from PHP to Java. In the old code, I had rolled my own ORM, which wasn't optimal for a number of reasons. Although I might start to tweak things later, and maybe end up implementing things myself again, for now I'd like to use an off-the-shelf ORM and JPA on my entity classes.
Now, there's one thing about the database layout that I don't know how to express in JPA:
I have a Node and an Edge table storing a graph (a DAG, if it matters). Each node may optionally reference one other entity from the database. These entites may be refrenced multiple times throughout the graph and there may also be "orphaned" entites, which wouldn't be accesible for the user, but which may make sense to keep at least for a while.
These objects are not at all related in terms of inheritance etc. but have a natural hierarchy, similar to Customer->Site->Floor->Room. In fact, years ago, I started out with just foreign key fields pointing to the "parent" objects. However, this hierarchy isn't flexible enough and started falling apart.
For example, I want to allow users to group objects in folders, some objects can have multiple "parents" and also the relations change over time. I need to keep track of how the relations used to be, so the edegs of the graph have a timespan associated with them, that states from when to when that edge was valid.
The link from a node to an object is stored in two columns of the node table, one carries the id in the foreign table, one carries its name. For example (some columns omitted):
table Node:
+--------+-------+----------+
| ixNode | ixRef | sRefType |
+--------+-------+----------+
| 1 | NULL | NULL | <-- this is what a "folder" would look like
| 2 | 17 | Source |
| 3 | 58 | Series | <-- there's seven types of related objects so far
+--------+-------+----------+
table Source (excerpt):
+----------+--------------------+
| ixSource | sName |
+----------+--------------------+
| 16 | 4th floor breaker |
| 17 | 5th floor breaker |
| 18 | 6th floor breaker |
+----------+--------------------+
There might be a different solution than using JPA. I could change something about the table layout or introduce a new table etc. However, I have thought about this a lot already and the table structure seems OK to me. Maybe there's also a third way that I didn't think of.
I think you've already hit on an answer. Create an abstract class (either #Entity or #MappedSuperclass) and have the different types extend it.
Something like this might work
#MappedSuperclass
#Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Edge {
// . . .
#OneToMany
Collection<Node> nodes;
}
#Entity
public class Source extends Edge {
}
#Entity public class Series extends Edge {
}
#Entity
public class Node {
// . . .
#ManyToOne
Edge edge;
}
I understand you might not want to imply a relationship between the Source and Series, but extending a common abstract (table-less) class is the only way I can think of to do what you want.
InheritanceType.TABLE_PER_CLASS will keep Source and Series in separate tables (you could use SINGLE_TABLE to do something like the previous answer).
If this isn't what you're looking for, many JPA providers provide a tool that creates mappings based on an existing set of tables. In OpenJPA it's called the ReverseMappingTool [1]. The tool will generate Java source files that you can use as a starting point for your mappings. I suspect Hibernate or EclipseLink have something similar, but you could just use the OpenJPA one and use the entity definitions with a different provider (the tool doesn't generate any OpenJPA specific code as far as I know).
[1] http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_pc_reverse
The answer would be:
inheritance (as suggested already by Mike)
plus #DiscriminatorColumn to provide information which column stores the information about which subclass should be used: sxRef. The only doubt I see is the "sxRef" being a nullable column. I guess that it's forbidden.
Have you looked at the #Any annotation? It's not part of JPA but is a Hibernate Annotation extension to it.
How much information is stored in the Source and Series tables? Is it just a name? If so, you could combine them into one table, and add a "type" column. Your Node table would lose its sRefType, and you would have a new table that looks like this:
ixSource sName sType
16 4th floor breaker SOURCE
17 5th floor breaker SOURCE
18 6th floor breaker SOURCE
19 1st floor widget SERIES
20 2nd floor widget SERIES
This table would replace the Source and Series tables. Do Source and Series both belong to a superclass? That would be a natural name for this table.